C# 如何更改 XML 节点值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11903552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to change XML node values
提问by RJ.
I have an XML (this is exactly what it looks like):
我有一个 XML(这正是它的样子):
<PolicyChangeSet schemaVersion="2.1" username="" description="">
<Attachment name="" contentType="">
<Description/>
<Location></Location>
</Attachment>
</PolicyChangeSet>
This is on the user's machine.
这是在用户的机器上。
I need to add values to each node: username, description, attachment name, contenttype, and location.
我需要为每个节点添加值:用户名、描述、附件名称、内容类型和位置。
This is what I have so far:
这是我到目前为止:
string newValue = string.Empty;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNode node = xmlDoc.SelectSingleNode("/PolicyChangeSet");
node.Attributes["username"].Value = AppVars.Username;
node.Attributes["description"].Value = "Adding new .tiff image.";
node.Attributes["name"].Value = "POLICY";
node.Attributes["contentType"].Value = "content Typeeee";
//node.Attributes["location"].InnerText = "zzz";
xmlDoc.Save(filePath);
Any help?
有什么帮助吗?
采纳答案by RJ.
Got it with this -
明白了——
xmlDoc.Load(filePath);
XmlNode node = xmlDoc.SelectSingleNode("/PolicyChangeSet");
node.Attributes["username"].Value = AppVars.Username;
node.Attributes["description"].Value = "Adding new .tiff image.";
node = xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment");
node.Attributes["name"].Value = "POLICY";
node.Attributes["contentType"].Value = "content Typeeee";
xmlDoc.Save(filePath);
回答by Jan
With XPath. XmlNode node = xmlDoc.SelectSingleNode("/PolicyChangeSet");selects your root node.
使用 XPath。XmlNode node = xmlDoc.SelectSingleNode("/PolicyChangeSet");选择您的根节点。
回答by Robert Zahm
In your SelectSingleNode method, you need to provide an XPath expression the find the node that you are looking to select. If you Google XPath you will find many resources for this.
在您的 SelectSingleNode 方法中,您需要提供一个 XPath 表达式来查找您要选择的节点。如果您使用 Google XPath,您会找到许多相关资源。
http://www.csharp-examples.net/xml-nodes-by-name/
http://www.csharp-examples.net/xml-nodes-by-name/
If you need to add these to each node, you can start at the top and iterate over all of the children.
如果您需要将这些添加到每个节点,您可以从顶部开始并遍历所有子节点。
http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.aspx
http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.aspx
回答by Nickon
Use LINQ To XML:)
使用 LINQ To XML:)
XDocument doc = XDocument.Load(path);
IEnumerable<XElement> policyChangeSetCollection = doc.Elements("PolicyChangeSet");
foreach(XElement node in policyChangeSetCollection)
{
node.Attribute("username").SetValue(someVal1);
node.Attribute("description").SetValue(someVal2);
XElement attachment = node.Element("attachment");
attachment.Attribute("name").SetValue(someVal3);
attachment.Attribute("contentType").SetValue(someVal4);
}
doc.Save(path);
回答by Ramzay
xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment/Description").InnerText = "My Desciption";
xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment/Location").InnerText = "My Location";
回答by Mohan Krishna Eluri
For setting value to XmlNode:
XmlNode node = xmlDoc.SelectSingleNode("/PolicyChangeSet");
node["username"].InnerText = AppVars.Username;
node["description"].InnerText = "Adding new .tiff image.";
node["name"].InnerText = "POLICY";
node["contentType"].InnerText = "content Typeeee";
For Getting value to XmlNode:
username=node["username"].InnerText

