我正在尝试打开这样的 xmldocument:
var doc = new XDocument("c:\\temp\\contacts.xml");
var reader = doc.CreateReader();
var namespaceManager = new XmlNamespaceManager(reader.NameTable);
namespaceManager.AddNamespace("g", g.NamespaceName);
var node = doc.XPathSelectElement("/Contacts/Contact/g:Name[text()='Patrick Hines']", namespaceManager);
node.Value = "new name Richard";
doc.Save("c:\\temp\\newcontacts.xml");
我在第一行返回一个错误:
Non whitespace characters cannot be added to content.
xml 文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Contacts xmlns:g="http://something.com">
<Contact>
<g:Name>Patrick Hines</g:Name>
<Phone>206-555-0144</Phone>
<Address>
<street>this street</street>
</Address>
</Contact>
</Contacts>