打开一个Xml文件并更改一些内容 [英] Open an Xml File and change somethin
本文介绍了打开一个Xml文件并更改一些内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<other>
<Data key="Name">Hans</Data>
<other>
<configuration>
你好,我有一个小问题。我喜欢改变"汉斯"。到"约瑟夫"用C#。问题在于,我想改变的是在两个"数据"之间。每日新闻。我可以更改密钥而不是标签内的值。
Hello there, I have a litle problem. I d'like to change "Hans" to "Josef" with C#. The problem is that the thing i d'like to change is between the two "Data" tages. I can change the key but not the value inside the tags.
谢谢
Sunilplayer
Sunilplayer
推荐答案
你好,Sunilplayer。
这个示例代码可以帮助你:
Hello, Sunilplayer.
This sample code can help you:
var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<other>
<Data key=""Name"">Hans</Data>
</other>
</configuration>";
var xDocument = XDocument.Parse(xml);
var xElement = xDocument.XPathSelectElement("//Data[@key='Name']");
xElement.Value = "Josef";
然后xDocument.ToString() 将返回:
Then xDocument.ToString() will return:
<configuration>
<other>
<Data key="Name">Josef</Data>
</other>
</configuration>
这篇关于打开一个Xml文件并更改一些内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文