修改在C#中的XML现有的内容 [英] Modify XML existing content in C#

查看:906
本文介绍了修改在C#中的XML现有的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用途:[?]。我打算创建的XmlTextWriter和修改/更新一些现有内容与XmlNode的的SelectSingleNode(),node.ChildNode一个XML文件中的InnerText =成才等

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

在我创建XmlTextWriter的XML文件如下图所示。

After I created the XML file with XmlTextWriter as below.

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

我练低于code。但未能拯救我的XML文件中。

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");  

我觉得这是不好的,产生像我的方式。
有没有关于它的任何建议?
我不清楚两者的XmlTextWriter和XmlNode的在同一个项目的概念和使用方法。
感谢您的阅读和回复。

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.

推荐答案

好吧,如果你想更新XML的一个节点,在的XmlDocument 是好的 - 你needn T选用的XmlTextWriter

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");

这篇关于修改在C#中的XML现有的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆