我有一个像这样的XML文件
<?xml version=""1.0"" encoding=""UTF-8""?>
<SharePointFormDataResponse xmlns =""http:///"">
<CustomerName>xyz</CustomerName>
<PMTREV2012>
<Transaction>
<Title>Full payment</Title>
<Amount>321323231</Amount>
<Month>Feb</Month>
<Year>2017</Year>
</Transaction>
<Transaction>
<Title>Full payment</Title>
<Amount>321323231</Amount>
<Month>Feb</Month>
<Year>2017</Year>
</Transaction>
</PMTREV2012>
<INTREV2012>
<Transaction>
<TransactionType>Full payment</TransactionType>
<Amount>321323231</Amount>
<Month>Feb</Month>
<Year>2017</Year>
</Transaction>
<Transaction>
<TransactionType>Full payment</TransactionType>
<Amount>321323231</Amount>
<Month>Feb</Month>
<Year>2017</Year>
</Transaction>
</INTREV2012>
</SharePointFormDataResponse>
我正在使用以下代码将此XML转换为数据集
XmlNodeReader xmlReader = new XmlNodeReader(altitudeResponse);
dsAltitude.ReadXml(xmlReader);
当我执行此操作时,所有事务节点将合并到一个数据表中。但我希望它们基于父节点位于不同的表中。我有什么方法可以实现这个目标吗?
答案 0 :(得分:0)
XmlNodeReader接受XmlNode
作为构造函数参数。只需将相应的父节点传递给您正在填充的每个表:
XmlDocument doc = new XmlDocument();
doc.LoadXml(altitudeResponse);
var parents = doc.DocumentElement.ChildNodes.Cast<XmlNode>()
.Where(n => n.Name != "CustomerName");
foreach(var parentNode in parents)
{
using (XmlNodeReader xmlReader = new XmlNodeReader(parentNode))
{
DataSet ds = new DataSet();
ds.ReadXml(xmlReader);
DataTable dt = ds.Tables[0].Copy();
dt.TableName = parentNode.Name;
dsAltitude.Tables.Add(dt);
}
}
不幸的是,如果没有架构,您无法直接将数据读入DataTable - 您应该使用DataSet。
输出 - 带有两个DataTables的DataSet:
{
"PMTREV2012": [
{
"Title": "Full payment",
"Amount": "321323231",
"Month": "Feb",
"Year": "2017"
},
{
"Title": "Full payment",
"Amount": "321323231",
"Month": "Feb",
"Year": "2017"
}
],
"INTREV2012": [
{
"TransactionType": "Full payment",
"Amount": "321323231",
"Month": "Feb",
"Year": "2017"
},
{
"TransactionType": "Full payment",
"Amount": "321323231",
"Month": "Feb",
"Year": "2017"
}
]
}
答案 1 :(得分:0)
试试这个..
这个有点笨重,但会产生所需的输出
public static void ReadXMLResponse()
{
XDocument doc = XDocument.Load(@"D:\\Response.xml");
DataSet ds = new DataSet();
System.Data.DataTable dt = new System.Data.DataTable("INTREV2012");
dt.Columns.Add("col1");
dt.Columns.Add("Amount");
dt.Columns.Add("Month");
dt.Columns.Add("Year");
dt = doc.Descendants("INTREV2012").Descendants("Transaction")
.Select(x =>
{
var row = dt.NewRow();
row.SetField<string>("col1", (string)x.Element("TransactionType").Value);
row.SetField<string>("Amount", (string)x.Element("Amount").Value);
row.SetField<string>("Month", (string)x.Element("Month").Value);
row.SetField<string>("Year", (string)x.Element("Year").Value);
return row;
}).CopyToDataTable();
ds.Tables.Add(dt);
dt = new System.Data.DataTable("PMTREV2012");
dt.Columns.Add("col1");
dt.Columns.Add("Amount");
dt.Columns.Add("Month");
dt.Columns.Add("Year");
dt = doc.Descendants("PMTREV2012").Descendants("Transaction")
.Select(x =>
{
var row = dt.NewRow();
row.SetField<string>("col1", (string)x.Element("Title").Value);
row.SetField<string>("Amount", (string)x.Element("Amount").Value);
row.SetField<string>("Month", (string)x.Element("Month").Value);
row.SetField<string>("Year", (string)x.Element("Year").Value);
return row;
}).CopyToDataTable();
ds.Tables.Add(dt);
Console.WriteLine(ds.Tables.Count);
}