Xml to string list

I have some code that I need to put in a string list in C #, and I am reading this code from XML files, and its location looks something like ...

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

How can I put each account tag in a list of strings? from the first <account> / account>

Please DO NOT tell me to go to the link below, as it does NOT work! How to read an XML file and write to List <gt ;?

So far I have tried the code below and the list of strings just stays empty

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

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

            this._accounts = list;
+4
source share
4 answers

This will work on your example (but you need to close this tag <motto>Hello Everyone>

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

EDIT as the answer to your question:

Is there a way to get id and specal_id on a separate line?

currentNode.Attributes["YourAttributeName"].Value, .


, :

class Account
    {
        public string  accountXml { get; set; }
        public string Id { get; set; }
        public string Special_id { get; set; }
    }

:

   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;
 }
+2

Descendants Elements:

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

Elements ( <main>).
Descendants .

: <motto>Hello Everyone> <motto>Hello Everyone</motto>

+6

Use XPath to get the element account:

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();
}
+2
source

try it

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

        }
    }
}
0
source

Source: https://habr.com/ru/post/1598827/


All Articles