在C#中修改XML现有内容

发布于 2024-08-27 09:01:14 字数 737 浏览 4 评论 0原文

目的:我计划使用 XmlTextWriter 创建一个 XML 文件,并使用 XmlNode SelectSingleNode()、node.ChildNode[?].InnerText = someting 等修改/更新一些现有内容。

在我使用 XmlTextWriter 创建 XML 文件后,如下所示。

XmlTextWriter textWriter = new XmlTextWriter("D:\\learning\\cs\\myTest.xml", System.Text.Encoding.UTF8);

我练习了下面的代码。但未能保存我的 XML 文件。

XmlDocument doc = new XmlDocument();
doc.Load("D:\\learning\\cs\\myTest.xml");

XmlNode root = doc.DocumentElement;
XmlNode myNode;

myNode= root.SelectSingleNode("descendant::books");

....

textWriter.Close();

doc.Save("D:\\learning\\cs\\myTest.xml");  

我发现按照我的方式生产并不好。 有什么建议吗? 我不清楚同一项目中 XmlTextWriter 和 XmlNode 的概念和用法。 感谢您的阅读和回复。

Purpose: I plan to Create a XML file with XmlTextWriter and Modify/Update some Existing Content with XmlNode SelectSingleNode(), node.ChildNode[?].InnerText = someting, etc.

After I created the XML file with XmlTextWriter as below.

XmlTextWriter textWriter = new XmlTextWriter("D:\\learning\\cs\\myTest.xml", System.Text.Encoding.UTF8);

I practiced the code below. But failed to save my XML file.

XmlDocument doc = new XmlDocument();
doc.Load("D:\\learning\\cs\\myTest.xml");

XmlNode root = doc.DocumentElement;
XmlNode myNode;

myNode= root.SelectSingleNode("descendant::books");

....

textWriter.Close();

doc.Save("D:\\learning\\cs\\myTest.xml");  

I found it is not good to produce like my way.
Is there any suggestion about it?
I am not clear about the concepts and usage of both XmlTextWriter and XmlNode in the same project.
Thank you for reading and replies.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

葮薆情 2024-09-03 09:01:14

好吧,如果您想更新 XML 中的节点,XmlDocument 就可以了 - 您无需使用 XmlTextWriter

XmlDocument doc = new XmlDocument();
doc.Load("D:\\build.xml");
XmlNode root = doc.DocumentElement;
XmlNode myNode = root.SelectSingleNode("descendant::books");
myNode.Value = "blabla";
doc.Save("D:\\build.xml");

Well, If you want to update a node in XML, the XmlDocument is fine - you needn't use XmlTextWriter.

XmlDocument doc = new XmlDocument();
doc.Load("D:\\build.xml");
XmlNode root = doc.DocumentElement;
XmlNode myNode = root.SelectSingleNode("descendant::books");
myNode.Value = "blabla";
doc.Save("D:\\build.xml");
青巷忧颜 2024-09-03 09:01:14

如果您使用的是 Framework 3.5,请使用 LINQ to xml

using System.Xml.Linq;

XDocument xmlFile = XDocument.Load("books.xml"); 
var query = from c in xmlFile.Elements("catalog").Elements("book")    
            select c; 
foreach (XElement book in query) 
{
    book.Attribute("attr1").Value = "MyNewValue";
}
xmlFile.Save("books.xml");

Using LINQ to xml if you are using framework 3.5

using System.Xml.Linq;

XDocument xmlFile = XDocument.Load("books.xml"); 
var query = from c in xmlFile.Elements("catalog").Elements("book")    
            select c; 
foreach (XElement book in query) 
{
    book.Attribute("attr1").Value = "MyNewValue";
}
xmlFile.Save("books.xml");
吲‖鸣 2024-09-03 09:01:14

形成 XML 文件

XmlTextWriter xmlw = new XmlTextWriter(@"C:\WINDOWS\Temp\exm.xml",System.Text.Encoding.UTF8);
xmlw.WriteStartDocument();            
xmlw.WriteStartElement("examtimes");
xmlw.WriteStartElement("Starttime");
xmlw.WriteString(DateTime.Now.AddHours(0).ToString());
xmlw.WriteEndElement();
xmlw.WriteStartElement("Changetime");
xmlw.WriteString(DateTime.Now.AddHours(0).ToString());
xmlw.WriteEndElement();
xmlw.WriteStartElement("Endtime");
xmlw.WriteString(DateTime.Now.AddHours(1).ToString());
xmlw.WriteEndElement();
xmlw.WriteEndElement();
xmlw.WriteEndDocument();  
xmlw.Close();           

要编辑 Xml 节点,请使用以下代码

XmlDocument doc = new XmlDocument(); 
doc.Load(@"C:\WINDOWS\Temp\exm.xml"); 
XmlNode root = doc.DocumentElement["Starttime"]; 
root.FirstChild.InnerText = "First"; 
XmlNode root1 = doc.DocumentElement["Changetime"]; 
root1.FirstChild.InnerText = "Second"; 
doc.Save(@"C:\WINDOWS\Temp\exm.xml"); 

尝试此操作。这是 C# 代码。

Forming a XML file

XmlTextWriter xmlw = new XmlTextWriter(@"C:\WINDOWS\Temp\exm.xml",System.Text.Encoding.UTF8);
xmlw.WriteStartDocument();            
xmlw.WriteStartElement("examtimes");
xmlw.WriteStartElement("Starttime");
xmlw.WriteString(DateTime.Now.AddHours(0).ToString());
xmlw.WriteEndElement();
xmlw.WriteStartElement("Changetime");
xmlw.WriteString(DateTime.Now.AddHours(0).ToString());
xmlw.WriteEndElement();
xmlw.WriteStartElement("Endtime");
xmlw.WriteString(DateTime.Now.AddHours(1).ToString());
xmlw.WriteEndElement();
xmlw.WriteEndElement();
xmlw.WriteEndDocument();  
xmlw.Close();           

To edit the Xml nodes use the below code

XmlDocument doc = new XmlDocument(); 
doc.Load(@"C:\WINDOWS\Temp\exm.xml"); 
XmlNode root = doc.DocumentElement["Starttime"]; 
root.FirstChild.InnerText = "First"; 
XmlNode root1 = doc.DocumentElement["Changetime"]; 
root1.FirstChild.InnerText = "Second"; 
doc.Save(@"C:\WINDOWS\Temp\exm.xml"); 

Try this. It's C# code.

别闹i 2024-09-03 09:01:14

XmlTextWriter 通常用于生成(而不是更新)XML 内容。将 xml 文件加载到 XmlDocument 中时,不需要单独的编写器。

只需更新您选择的节点并 .Save() 该 XmlDocument。

The XmlTextWriter is usually used for generating (not updating) XML content. When you load the xml file into an XmlDocument, you don't need a separate writer.

Just update the node you have selected and .Save() that XmlDocument.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文