【问题标题】:XmlDocument how to insert new record into an existing XML FileXmlDocument 如何将新记录插入现有 XML 文件
【发布时间】:2020-07-15 02:12:57
【问题描述】:

我在网上关注了几个例子,但没有运气,不确定我的代码有什么问题。

我已经有 xml 文件,我将它加载到我的程序中并有一些记录为

<RETS>
  <Servers>
    <serverInfo type="Type1" LoginString="http://rets.Login" LoginUserName="Ret124" LoginPassword="Mypassword" RetsVersion="RETS/1.5"/>
    </Servers>
  <SearchStrings>
    <search type="Type1"><![CDATA[http://rets2_3/GetMetadata]]></search>
  </SearchStrings>  
</RETS>

然后我让用户添加新记录,它应该看起来像这样 = serverInfo type="Type2" LoginString="http:www.xml.com" LoginUserName="Re34555" 等

XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("RETSDictionary.xml");
            XmlNode node = xmlDoc.SelectSingleNode("RETS/Servers/serverInfo");
            node.Attributes["type"].Value = m_type; // these values coming for text field 
            node.Attributes["LoginString"].Value = m_loginString;
            node.Attributes["LoginPassword"].Value = m_loginPassword;
            node.Attributes["LoginUserName"].Value = m_loginUserName;
            node.Attributes["RetsVersion"].Value = m_retsVersion;


            try {
                xmlDoc.Save("RETSDictionary.xml");
                m_isSuccessful = true;
                m_message = "New RETS Server saved.";
            }
            catch (Exception ex) {
                m_isSuccessful = false;
                m_message = ex.Message;
            }

所以当它点击保存时没有任何反应!

【问题讨论】:

  • 您确定从文档中获取数据吗?您是否尝试过显示数据?
  • 请在添加新的 XML 片段后分享您想要的输出。
  • rets.Login" LoginUserName="Ret124" LoginPassword="Mypassword" RetsVersion="RETS/1.5"/> 2222.Login" LoginUserName="tyy" LoginPassword="Mypassword2" RetsVersion="RETS/1.5"/> 55555.Login" LoginUserName="454" LoginPassword="Mypassword3 " RetsVersion="RETS/1.5"/> rets2_3/GetMetadata]]></search>
  • 是的,我可以显示日期

标签: c# xml xmldocument


【解决方案1】:

尝试创建一个新元素,然后将其附加到服务器节点。

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("RETSDictionary.xml");
XmlNode serversNode = xmlDoc.SelectSingleNode("RETS/Servers");
XmlElement node = xmlDoc.CreateElement("serverInfo");
node.SetAttribute("type", m_type); // these values coming for text field 
node.SetAttribute("LoginString", m_loginString);
node.SetAttribute("LoginPassword", m_loginPassword);
node.SetAttribute("LoginUserName", m_loginUserName);
node.SetAttribute("RetsVersion", m_retsVersion);
serversNode.AppendChild(node);

【讨论】:

    【解决方案2】:

    最好使用 LINQ to XML。这个 API 已经存在十多年了。它取代了以前的 .Net Framework XML API。

    c#

    void Main()
    {
        const string fileName = @"e:\temp\RETSDictionary.xml";
    
        XDocument xdoc = XDocument.Load(fileName);
    
        // compose new fragment
        XElement fragment = new XElement("serverInfo",
                new XAttribute("type", "Type2"),
                new XAttribute("LoginString", "2222.Login"),
                new XAttribute("LoginUserName", "tyy"),
                new XAttribute("LoginPassword", "Mypassword2"),
                new XAttribute("RetsVersion", "RETS/1.5")
            );
    
        // add new fragment to a proper location
        xdoc.Descendants("Servers").LastOrDefault().Add(fragment);
    
        // save back to XML file
        xdoc.Save(fileName);
    }
    

    输出

    <RETS>
      <Servers>
        <serverInfo type="Type1" LoginString="http://rets.Login" LoginUserName="Ret124" LoginPassword="Mypassword" RetsVersion="RETS/1.5" />
        <serverInfo type="Type2" LoginString="2222.Login" LoginUserName="tyy" LoginPassword="Mypassword2" RetsVersion="RETS/1.5" />
      </Servers>
      <SearchStrings>
        <search type="Type1"><![CDATA[http://rets2_3/GetMetadata]]></search>
      </SearchStrings>
    </RETS>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 2016-08-15
      • 2014-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多