XmlDocument 如何将新记录插入现有 XML 文件

XmlDocument how to insert new record into an existing XML File

我在网上遵循了几个示例,但没有成功,不确定我的代码有什么问题。

我已经有 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 类型="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;
            }

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

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

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

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

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

Output

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