【问题标题】:How can I can Parse the xml into a list variable如何将 xml 解析为列表变量
【发布时间】:2021-03-15 11:52:55
【问题描述】:

我正在尝试将 xml 从其根标记解析为列表变量。我在下面给出了xml文件格式。我想将 xml 的全部内容放入一个列表变量中,不包括第一行

    <?xml version="1.0" encoding="utf-8" ?>
<Root>
  <Database>
    Data Source
  </Database>
  <FromEmailDetails>
    <UserName>
      testuser
    </UserName>
    <Password>
      myPassword
    </Password>
    <FromAddress>
      test@gmail.com
    </FromAddress>
    <Server>
      myserver
    </Server>
    <port>
      80
    </port>
  </FromEmailDetails>
  <FileFormat>
    XLSX
  </FileFormat>
  <ExportFolder>
    C:\NOTNEED\
  </ExportFolder>
  <Customer>
    <SQL ID="GYSQL">
      Select * from customer where code ='GYSQL'
    </SQL>
    <MailBody>
      Please find attached Report
    </MailBody>
    <Address>customer1@mail.com</Address>
    <Address>customer2@mail.com</Address>
  </Customer>
  <Customer>
    <SQL ID="TSSQL">
      Select * from customer where code ='TSSQL'
    </SQL>
    <MailBody>
      Please find attached Report
    </MailBody>
    <Address>customer3@mail.com</Address>
    <Address>customer4@mail.com</Address>
    <Address>customer5@mail.com</Address>
  </Customer>
</Root>

我正在尝试将 xml 解析为列表

var doc = XDocument.Parse(textXML);
var contents = xml  .ToList();  // Please help here how can I store the xml content as list

【问题讨论】:

    标签: c# xml-parsing


    【解决方案1】:

    使用 Xml Linq:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    
    namespace ConsoleApplication176
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                XDocument doc = XDocument.Load(FILENAME);
                XElement root = doc.Root;
                DataBase db = new DataBase(root);
      
            }
        }
        public class DataBase
        {
            public string database { get; set; }
            public Email email { get; set; }
            public string fileFormat { get; set; }
            public string exportFolder { get; set; }
            public List<Customer> customers { get; set; }
            public DataBase(XElement root)
            {
                database = ((string)root.Element("Database")).Trim();
                fileFormat = ((string)root.Element("FileFormat")).Trim();
                exportFolder = ((string)root.Element("ExportFolder")).Trim();
                email = new Email(root.Element("FromEmailDetails"));
                customers = root.Elements("Customer").Select(x => new Customer(x)).ToList();
    
            }
        }
        public class Email
        {
            public string username { get; set; }
            public string password { get; set; }
            public string fromAddress { get; set; }
            public string server { get; set; }
            public int port { get; set; }
    
            public Email(XElement email)
            {
                username = ((string)email.Element("UserName")).Trim();
                password = ((string)email.Element("Password")).Trim();
                fromAddress = ((string)email.Element("FromAddress")).Trim();
                server = ((string)email.Element("Server")).Trim();
                port = (int)email.Element("port");
            }
        }
        public class Customer
        {
            public string id { get; set; }
            public string sql { get; set; }
            public string mailBody { get; set; }
            public List<string> addresses { get; set; }
    
            public Customer(XElement customer)
            {
                id = ((string)customer.Element("SQL").Attribute("ID")).Trim();
                sql = ((string)customer.Element("SQL")).Trim();
                mailBody = ((string)customer.Element("SQL")).Trim();
                addresses = customer.Elements("Address").Select(x => ((string)x).Trim()).ToList();
            }
        }
      
    }
    

    【讨论】:

      【解决方案2】:

      这应该可行:

      foreach (var entry in doc.Descendants()) 
      {
          contents.Add(entry);
      }
      

      【讨论】:

      • 如何声明内容变量
      • 列表 内容 = 新列表(); @systemthreep
      猜你喜欢
      • 2012-05-22
      • 1970-01-01
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 2015-04-13
      • 2017-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多