【问题标题】:XML field replacement in C#C# 中的 XML 字段替换
【发布时间】:2009-10-07 16:52:38
【问题描述】:

好的,所以,我有一个如下所示的 xml 文件:

<?xml version="1.0"?>
<Users>
  <User ID="1">
    <nickname>Tom</nickname>
    <password>a password</password>
    <host>anemail@hello.com</host>
    <email>anemail</email>
    <isloggedin>false</isloggedin>
    <permission>10</permission>
  </User>
  <User ID="2">
    <nickname>ohai</nickname>
    <password>sercret</password>
    <host>my@host</host>
    <email>my@email</email>
    <isloggedin>false</isloggedin>
    <permission>1</permission>
  </User>
<Users>

现在,首先,我将返回他们的 ID 号,所以,我有“2”。 从那以后,我需要进入并编辑其中的字段,然后重新保存 xml。 所以基本上我需要的是打开文件,找到用户 ID =“2”的信息,然后用用户 2 内部的不同值重新保存 xml,而不影响文档的其余部分。

示例:

  <User ID="2">
    <nickname>ohai</nickname>
    <password>sercret</password>
    <host>my@host</host>
    <email>my@email</email>
    <isloggedin>false</isloggedin>
    <permission>1</permission>
  </User>

//在这里进行修改,最终得到

  <User ID="2">
    <nickname>ohai</nickname>
    <password>somthing that is different than before</password>
    <host>the most current host that they were seen as</host>
    <email>my@email</email>
    <isloggedin>false</isloggedin>
    <permission>1</permission>
  </User>

等等

总结: 我需要打开一个文本文件,通过 ID 号返回信息,编辑信息,重新保存文件。不影响用户 2 以外的任何内容

~谢谢!

【问题讨论】:

    标签: c# xml replace


    【解决方案1】:

    有多种方法可以做到这一点 - 这是使用 XmlDocument,它适用于 .NET 1.x 及更高版本,只要您的 XML 文档不太长,它就可以正常工作:

    // create new XmlDocument and load file
    XmlDocument xdoc = new XmlDocument();
    xdoc.Load("YourFileName.xml");
    
    // find a <User> node with attribute ID=2
    XmlNode userNo2 = xdoc.SelectSingleNode("//User[@ID='2']");
    
    // if found, begin manipulation    
    if(userNo2 != null)
    {
       // find the <password> node for the user
       XmlNode password = userNo2.SelectSingleNode("password");
       if(password != null)
       {
          // change contents for <password> node 
          password.InnerText = "somthing that is different than before";
       }
    
       // find the <host> node for the user
       XmlNode hostNode = userNo2.SelectSingleNode("host");
       if(hostNode != null)
       {
          // change contents for <host> node 
          hostNode.InnerText = "the most current host that they were seen as";
       }
    
       // save changes to a new file (or the old one - up to you)
       xdoc.Save("YourFileNameNew.xml");
    }
    

    如果您使用的是 .NET 3.5 及更高版本,您还可以检查 Linq-to-XML 以获得一种可能更简单的方式来操作您的 XML 文档。

    马克

    【讨论】:

      【解决方案2】:

      查看这个问题,应该使用 Linq-to-XML 得到答案。这涉及写出到控制台窗口,但原理是一样的。

      Linq to XML - update/alter the nodes of an XML Document

      【讨论】:

        【解决方案3】:

        您可以为此使用 XmlDocument:

        var doc = new XmlDocument();
        doc.Load("1.xml");
        var node = doc.SelectSingleNode(@"//User[@ID='2']");
        node.SelectSingleNode("password").InnerText="terces";
        doc.Save("1.xml");
        

        【讨论】:

        • node.SelectSingleNode("password").InnerText 如果找不到“密码”节点,这是很危险的——你会撞到 NullReferenceException
        • 是的,你是对的。为简洁起见,我在此示例中省略了它,但如果我编写更容易出错的示例会更好。
        猜你喜欢
        • 1970-01-01
        • 2015-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        • 2012-04-13
        • 2018-04-12
        相关资源
        最近更新 更多