C# XML到字符串列表

C# XML到字符串列表,c#,xml,C#,Xml,我有一些代码需要放入C#中的字符串列表中,我正在从XML文件中读取这些代码,其布局如下所示 <?xml version="1.0"?> <accountlist> <main> <account id="1" special_id="4923959"> <username>Adam</username> <motto>Hello Everyo

我有一些代码需要放入C#中的字符串列表中,我正在从XML文件中读取这些代码,其布局如下所示

<?xml version="1.0"?>
<accountlist>
    <main>
        <account id="1" special_id="4923959">
            <username>Adam</username>
            <motto>Hello Everyone>
            <money>1004</money>
            <friends>394</friends>
            <rareid>9</rareid>
            <mission>10</mission>
        </account>
    </main>
</accountlist>

到目前为止,我已经尝试了下面的代码,字符串列表仍然是空的

XDocument doc = XDocument.Parse(this._accountsFile);

            List<string> list = doc.Root.Elements("account")
                               .Select(element => element.Value)
                               .ToList();

            this._accounts = list;
XDocument doc=XDocument.Parse(this.\u accountsFile);
列表=单据根元素(“账户”)
.Select(元素=>element.Value)
.ToList();
这是。_账户=列表;

首先使用XPath获取
帐户
元素:

using System.Xml.XPath;    

XDocument doc = XDocument.Parse(xml);

foreach(var account in doc.XPathSelectElements("accountlist/main/account")){
        List<string> list = account.Descendants()
                       .Select(element => element.Value)
                       .ToList();
}
使用System.Xml.XPath;
XDocument doc=XDocument.Parse(xml);
foreach(doc.XPathSelectElements(“accountlist/main/account”)中的var帐户){
List List=帐户。子体()
.Select(元素=>element.Value)
.ToList();
}

这将适用于您的示例(但您需要关闭此标记
大家好>

      public List<string> GetAccountsAsXmlList(string filePath)
      {
        XmlDocument x = new XmlDocument();

        x.Load(filePath);
        List<string> result = new List<string>();
        XmlNode currentNode;
        foreach (var accountNode in x.LastChild.FirstChild.ChildNodes)
        {
            currentNode = accountNode  as XmlNode;
            result.Add(currentNode.InnerXml);
        }
          return result;
      }

然后:

   public List<Account> GetAccountsAsXmlList(string filePath)
   {
     XmlDocument x = new XmlDocument();

     x.Load(filePath);
     List<Account> result = new List<Account>();
     XmlNode currentNode;
     foreach (var accountNode in x.LastChild.FirstChild.ChildNodes)
     {
         currentNode = accountNode as XmlNode;
         result.Add(new Account
         {
             accountXml = currentNode.InnerXml,
             Id = currentNode.Attributes["id"].Value,
             Special_id = currentNode.Attributes["special_id"].Value,
         });
     }
      return result;
 }
public List GetAccountsAsXmlList(字符串文件路径)
{
XmlDocument x=新的XmlDocument();
x、 加载(文件路径);
列表结果=新列表();
XmlNode当前节点;
foreach(x.LastChild.FirstChild.ChildNodes中的var accountNode)
{
currentNode=accountNode作为XmlNode;
结果.添加(新帐户)
{
accountXml=currentNode.InnerXml,
Id=currentNode.Attributes[“Id”]。值,
特殊id=currentNode.Attributes[“特殊id”]。值,
});
}
返回结果;
}

您将不得不使用
子体
而不是
元素

List<string> list = doc.Root.Descendants("account").Descendants()
                   .Select(element => element.Value)
                   .ToList();
List List=doc.Root.substands(“帐户”).substands()
.Select(元素=>element.Value)
.ToList();
元素
仅返回元素的子元素(对于根元素,这意味着
)。
子体
返回元素中的整个树

同时:您必须将标签
Hello Everyone>
修改为
Hello Everyone

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication37
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
                "<?xml version=\"1.0\"?>" +
                "<accountlist>" +
                  "<main>" +
                    "<account id=\"1\" special_id=\"4923959\">" +
                      "<username>Adam</username>" +
                      "<motto>" +
                        "Hello Everyone>" +
                        "<money>1004</money>" +
                        "<friends>394</friends>" +
                        "<rareid>9</rareid>" +
                        "<mission>10</mission>" +
                      "</motto>" +
                      "</account>" +
                  "</main>" +
                "</accountlist>";

            XDocument doc = XDocument.Parse(input);

            var results = doc.Descendants("accountlist").Select(x => new {
                id = x.Element("main").Element("account").Attribute("id").Value,
                special_id = x.Element("main").Element("account").Attribute("special_id").Value,
                username = x.Element("main").Element("account").Element("username").Value,
                motto = x.Element("main").Element("account").Element("motto").FirstNode.ToString(),
                money = x.Element("main").Element("account").Element("motto").Element("money").Value,
                friends = x.Element("main").Element("account").Element("motto").Element("friends").Value,
                rareid = x.Element("main").Element("account").Element("motto").Element("rareid").Value,
                mission = x.Element("main").Element("account").Element("motto").Element("mission").Value,
            }).ToList();

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序37
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串输入=
"" +
"" +
"" +
"" +
“亚当”+
"" +
“大家好>”+
"1004" +
"394" +
"9" +
"10" +
"" +
"" +
"" +
"";
XDocument doc=XDocument.Parse(输入);
var results=doc.subjects(“accountlist”)。选择(x=>new{
id=x.Element(“主”).Element(“账户”).Attribute(“id”).Value,
特殊id=x.Element(“main”).Element(“account”).Attribute(“特殊id”).Value,
用户名=x.Element(“主”).Element(“帐户”).Element(“用户名”).Value,
座右铭=x.Element(“main”).Element(“account”).Element(“座右铭”).FirstNode.ToString(),
金钱=x元素(“主要”)。元素(“账户”)。元素(“座右铭”)。元素(“金钱”)。价值,
friends=x.Element(“主要”).Element(“账户”).Element(“座右铭”).Element(“朋友”).Value,
rareid=x.Element(“main”).Element(“account”).Element(“座右铭”).Element(“rareid”).Value,
使命=x.要素(“主要”).要素(“账户”).要素(“座右铭”).要素(“使命”).价值,
}).ToList();
}
}
}

什么“不起作用”包含?您应该显示您尝试过的代码。它不会在列表中添加任何内容。列表保持为空。我已将我的问题更新到我尝试过的代码中。这只是您的示例吗,您的
Hello Everyone>
元素已损坏?
帐户
不是根元素,但是,您是否尝试过选择
元素f首先,然后获取该帐户的
元素
元素?有没有一种方法可以在单独的字符串中获取id和specal_id?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication37
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
                "<?xml version=\"1.0\"?>" +
                "<accountlist>" +
                  "<main>" +
                    "<account id=\"1\" special_id=\"4923959\">" +
                      "<username>Adam</username>" +
                      "<motto>" +
                        "Hello Everyone>" +
                        "<money>1004</money>" +
                        "<friends>394</friends>" +
                        "<rareid>9</rareid>" +
                        "<mission>10</mission>" +
                      "</motto>" +
                      "</account>" +
                  "</main>" +
                "</accountlist>";

            XDocument doc = XDocument.Parse(input);

            var results = doc.Descendants("accountlist").Select(x => new {
                id = x.Element("main").Element("account").Attribute("id").Value,
                special_id = x.Element("main").Element("account").Attribute("special_id").Value,
                username = x.Element("main").Element("account").Element("username").Value,
                motto = x.Element("main").Element("account").Element("motto").FirstNode.ToString(),
                money = x.Element("main").Element("account").Element("motto").Element("money").Value,
                friends = x.Element("main").Element("account").Element("motto").Element("friends").Value,
                rareid = x.Element("main").Element("account").Element("motto").Element("rareid").Value,
                mission = x.Element("main").Element("account").Element("motto").Element("mission").Value,
            }).ToList();

        }
    }
}