【问题标题】:How to change XML node values如何更改 XML 节点值
【发布时间】:2012-08-07 20:48:43
【问题描述】:

我有一个 XML(这正是它的样子):

<PolicyChangeSet schemaVersion="2.1" username="" description="">
    <Attachment name="" contentType="">
        <Description/>
        <Location></Location>
    </Attachment>
</PolicyChangeSet>

这是在用户的机器上。

我需要为每个节点添加值:用户名、描述、附件名称、内容类型和位置。

这是我目前所拥有的:

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

有什么帮助吗?

【问题讨论】:

    标签: c# xml xmlnode


    【解决方案1】:

    使用 XPath。 XmlNode node = xmlDoc.SelectSingleNode("/PolicyChangeSet"); 选择你的根节点。

    【讨论】:

    • 那行得通:) ...我只能在 10 分钟内接受你的回答。谢谢一月!
    • 我如何为“位置”添加一个值?它只是在 > ... ?? 之间
    • Anytime :) 查看 XmlNode 的 InnerText 属性。
    • 顺便说一句,用户名和描述都可以正常工作,但是当我需要更改附件名称时出现错误,我用我的代码编辑了我的帖子,请看一下
    • 位置是一个节点,而不是一个属性。 XPath 也区分大小写。 node = xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment/Location"); 然后node.InnerText = "myLocation" 是去这里的路
    【解决方案2】:
    xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment/Description").InnerText = "My Desciption";
    xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment/Location").InnerText = "My Location";
    

    【讨论】:

      【解决方案3】:

      知道了 -

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

      【讨论】:

        【解决方案4】:

        使用 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);
        

        【讨论】:

          【解决方案5】:

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

          http://www.csharp-examples.net/xml-nodes-by-name/

          如果您需要将这些添加到每个节点,您可以从顶部开始并遍历所有子节点。

          http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.aspx

          【讨论】:

            【解决方案6】:
            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 
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2011-09-28
              • 2021-12-24
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多