【问题标题】:How to add multiple root elements to xml file and use it by appending contents multiple times如何将多个根元素添加到 xml 文件并通过多次附加内容来使用它
【发布时间】:2016-03-18 01:00:21
【问题描述】:

在我的例子中,我想将多个根节点添加到给定的 xml 架构中。所以我必须通过附加xml文件中的先前内容来多次附加不同的用户元素及其子节点。我的问题是如何添加多个根元素? (我可以添加一个根元素,但不知道如何添加下一个)。

这是 xml 架构

    <?xml version="1.0" encoding="utf-8"?>
<SessionId>
  <Scource>
    <User username="AB">
      <DOB>25/5/1980</DOB>
      <FirstName>AVS</FirstName>
      <LastName>WDW</LastName>
      <Location>FWAWE</Location>
    </User>
  <User username="AqB">
    <DOB>25/5/1980</DOB>
    <FirstName>AVS</FirstName>
    <LastName>WDW</LastName>
    <Location>FWAWE</Location>
  </User>
  </Scource>
</SessionId>

这是我的 C# 代码

   using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace FilteringNightwing
{
    class Test
    {
         static void Main(string[] args) {

            string fileLocation = "clients.xml";

            if (!File.Exists(fileLocation))
            {
                XmlTextWriter writer = new XmlTextWriter(fileLocation, null);
                writer.WriteStartElement("SessionId");
                writer.WriteEndElement();
                writer.Close();

            }

            // Load existing clients and add new 
            XElement xml = XElement.Load(fileLocation);
            xml.Add(new XElement("User",
            new XAttribute("username", "AqB"),
            new XElement("DOB", "25/5/1980"),
            new XElement("FirstName", "AVS"),
            new XElement("LastName", "WDW"),
            new XElement("Location", "FWAWE")));
            xml.Save(fileLocation);

        }

    }
}
  • 如何将“源”标签添加为另一个根元素。欢迎您提供所有答案。

【问题讨论】:

    标签: c# xml append xmlwriter


    【解决方案1】:

    您不能拥有多个根节点。解析以下内容会报错。

    XElement root = XElement.Parse("<a></a><b></b>");
    

    这也会出错:

    XElement root = XElement.Parse("<a></a><a></a>");
    

    如果您希望根节点有多个子节点,那很好。您可以拥有名称相同或不同的节点。如:

    <a>
      <b />
      <c>
        <d />
      </c>
      <b />
    </a>
    

    将新用户添加到Scource

    XElement xml = XElement.Load(fileLocation);
    XElement scource = xml.Element("Scource"); // Find Scource here
    scource.Add(new XElement("User", // Add new user to Scource here
        new XAttribute("username", "AqB"),
        new XElement("DOB", "25/5/1980"),
        new XElement("FirstName", "AVS"),
        new XElement("LastName", "WDW"),
        new XElement("Location", "FWAWE")));
    xml.Save(fileLocation);
    

    【讨论】:

    • 但在我的情况下,结构不像你给的那样。这就像 。我该怎么做?
    • @OreEraki 在他的回答中向您展示了如何。你找到你想要添加它的节点,或者之后,或者之前,或者父节点等等。然后使用新的XElement调用适当的方法。
    • @user0001 如果您希望将新用户添加到当前来源(我将编辑我的答案)
    • 是的,我想在源之间添加多个新用户。你能给我那个代码吗
    • @user0001 您的 XML 有 scource 而不是 source。所以我让它使用你的 xml 显示的内容。
    【解决方案2】:

    试试这个

        using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><SessionId><Source></Source></SessionId>";
                XDocument doc = XDocument.Parse(xml);
                XElement source = doc.Descendants("Source").FirstOrDefault();
    
                source.Add(new XElement("User", new object[] {
                    new XAttribute("username", "AqB"),
                    new XElement("DOB", "25/5/1980"),
                    new XElement("FirstName", "AVS"),
                    new XElement("LastName", "WDW"),
                    new XElement("Location", "FWAWE")
               }));
            }
        }
    }
    ​
    

    【讨论】:

      猜你喜欢
      • 2015-11-18
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2022-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多