使用ASP.NET读取XML [英] Reading XML using ASP.NET
问题描述
<?xml version =1.0?>
< DatabaseSubMenuReportExecution>
< Columns>
< C0> RoleName< / C0>
< C1> Grantee< / C1>
< C2> Grantor< / C2>
< C3> WhenGranted< / C3>
< / Columns>
<行>
< R0> ceuser< / R0>
< R1> coe4< / R1>
< R2> DBC< / R2>
< R3> 2016-07-19 09:24:22.0< / R3>
< / Rows>
<行>
< R0>迁移< / R0>
< R1> coe4< / R1>
< R2> DBC< / R2>
< R3> 2016-07-19 09:23:56.0< / R3>
< / Rows>
< / DatabaseSubMenuReportExecution>
对于上面的XML,所需的输出应该如下表所示。
如何实现这一目标?
RoleName --------受让人---------授予者---------- WhenGranted
Ceuser ------ ---- Coe4 ------------ DBC -------------- 2016-07-19 09:24:22.0
迁移--- ---- Coe4 ------------ DBC -------------- 2016-07-19 09:24:22.0
< br $> b $ b
我尝试过:
对于上面的XML所需的输出应该如下表。
如何实现这个目标?
RoleName -------- Grantee ---------授予者---------- WhenGranted
Ceuser --------- -Coe4 ------------ DBC -------------- 2016-07-19 09:24:22.0
迁移 - ----- Coe4 ------------ DBC -------------- 2016-07-19 09:24:22.0
看看例子:
//打开xml
XDocument xdoc = XDocument.Load(FullXmlFileNameWithPathAndExtension);
//创建新的数据表
DataTable dt = new DataTable();
//从列节点获取列
DataColumn [] cols = xdoc.Root.Element(Columns)。Descendants()
.Select(a => ; new DataColumn(a.Value,Type.GetType(System.String)))。ToArray();
//将列添加到数据表
dt.Columns.AddRange(cols);
//从行节点获取行
var rows = xdoc.Root.Descendants(Rows)
.Select((a,i)=> new {Index = i,Values = a.Descendants()。选择(b => b.Value).ToArray()});
//向数据表添加行
foreach(行中的var r)
{
dt.Rows.Add(r.Values);
}
//数据表已准备好使用
结果:
< pre lang =text> RoleName Grantee Grantor WhenGranted
ceuser coe4 DBC 2016-07-19 09:24:22.0
迁移coe4 DBC 2016-07-19 09:23 :56.0
注意:以上示例并不关心数据类型。所有数据都被视为文本(字符串)。
XmlDocument document = new XmlDocument();
// string xmlString = ; //如果要传递XML As String
// document.LoadXml(xmlString); //加载XML字符串
document.Load(@C:.... \ test.xml); //加载XML文件
XmlNodeList nodes = document.SelectNodes(/ root / string);
foreach(节点中的XmlNode项目)
{
string res = item.InnerText;
}
<?xml version =1.0encoding =utf-8?>
< root>
< string> ABC
< string> CYZ
<?xml version="1.0" ?> <DatabaseSubMenuReportExecution> <Columns> <C0>RoleName</C0> <C1>Grantee</C1> <C2>Grantor</C2> <C3>WhenGranted</C3> </Columns> <Rows> <R0>ceuser</R0> <R1>coe4</R1> <R2>DBC</R2> <R3>2016-07-19 09:24:22.0</R3> </Rows> <Rows> <R0>migration</R0> <R1>coe4</R1> <R2>DBC</R2> <R3>2016-07-19 09:23:56.0</R3> </Rows> </DatabaseSubMenuReportExecution> For the above XML the required output should be like below table. How to achieve this? RoleName--------Grantee---------Grantor----------WhenGranted Ceuser----------Coe4------------DBC--------------2016-07-19 09:24:22.0 Migration-------Coe4------------DBC--------------2016-07-19 09:24:22.0
What I have tried:
For the above XML the required output should be like below table.
How to achieve this?
RoleName--------Grantee---------Grantor----------WhenGranted
Ceuser----------Coe4------------DBC--------------2016-07-19 09:24:22.0
Migration-------Coe4------------DBC--------------2016-07-19 09:24:22.0
Take a look at example:
//open xml XDocument xdoc = XDocument.Load("FullXmlFileNameWithPathAndExtension"); //create new datatable DataTable dt = new DataTable(); //get columns from "Columns" node DataColumn[] cols = xdoc.Root.Element("Columns").Descendants() .Select(a => new DataColumn(a.Value, Type.GetType("System.String"))).ToArray(); //add columns to the datatable dt.Columns.AddRange(cols); //get rows from "Rows" nodes var rows = xdoc.Root.Descendants("Rows") .Select((a, i)=> new {Index = i, Values = a.Descendants().Select(b=> b.Value).ToArray()}); //add rows to the datatable foreach (var r in rows) { dt.Rows.Add(r.Values); } //datatable is ready to use
Result:
RoleName Grantee Grantor WhenGranted ceuser coe4 DBC 2016-07-19 09:24:22.0 migration coe4 DBC 2016-07-19 09:23:56.0
Note: above example doesn't care about data type. All data are treated as text (string).
XmlDocument document = new XmlDocument();
// string xmlString = ""; // If you are passing XML As String
// document.LoadXml(xmlString); // Load XML string
document.Load(@"C:....\test.xml"); // Load XML File
XmlNodeList nodes = document.SelectNodes("/root/string");
foreach (XmlNode item in nodes)
{
string res = item.InnerText;
}
<?xml version="1.0" encoding="utf-8" ?>
<root>
<string> ABC
<string> CYZ
这篇关于使用ASP.NET读取XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!