C# 如何更改XML节点值

C# 如何更改XML节点值,c#,xml,xmlnode,C#,Xml,Xmlnode,我有一个XML(它看起来就是这个样子): 有什么帮助吗?关于XPathXmlNode node=xmlDoc.SelectSingleNode(“/PolicyChangeSet”)选择根节点。在SelectSingleNode方法中,需要提供一个XPath表达式来查找要选择的节点。如果您使用Google XPath,您将找到许多相关资源 如果需要将它们添加到每个节点,可以从顶部开始,迭代所有子节点 我明白了- xmlDoc.Load(filePath); XmlNod

我有一个XML(它看起来就是这个样子):


有什么帮助吗?

关于XPath
XmlNode node=xmlDoc.SelectSingleNode(“/PolicyChangeSet”)选择根节点。

在SelectSingleNode方法中,需要提供一个XPath表达式来查找要选择的节点。如果您使用Google XPath,您将找到许多相关资源

如果需要将它们添加到每个节点,可以从顶部开始,迭代所有子节点

我明白了-

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);
使用LINQ转换为XML:)

XDocument doc=XDocument.Load(路径);
IEnumerable policyChangeSetCollection=doc.Elements(“PolicyChangeSet”);
foreach(policyChangeSetCollection中的XElement节点)
{
node.Attribute(“username”).SetValue(someVal1);
node.Attribute(“description”).SetValue(someVal2);
XElement附件=节点元素(“附件”);
附件.Attribute(“name”).SetValue(someVal3);
附件.Attribute(“contentType”).SetValue(someVal4);
}
单据保存(路径);

有效:)。。。我只能在10分钟内接受你的回答。谢谢,简!但是,我如何为“位置”添加值?就在…之间??Anytime:)查看XmlNode的
InnerText
属性。顺便说一下,用户名和说明正常工作,但当我需要更改附件名称时,我收到了一个错误,我用代码编辑了我的帖子,请查看位置是节点,而不是属性。XPath也是区分大小写的
node=xmlDoc.SelectSingleNode(“/PolicyChangeSet/Attachment/Location”)
然后
node.InnerText=“myLocation”
就是到这里的方法
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);
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);
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);
xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment/Description").InnerText = "My Desciption";
xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment/Location").InnerText = "My Location";
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