如何将 xml 解析为列表变量
How can I can Parse the xml into a list variable
我正在尝试将 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
这应该有效:
foreach (var entry in doc.Descendants())
{
contents.Add(entry);
}
使用 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();
}
}
}
我正在尝试将 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
这应该有效:
foreach (var entry in doc.Descendants())
{
contents.Add(entry);
}
使用 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();
}
}
}