[英]XML in foreach loop from dataset in C#
I have to create a very specific shape XML file dynamically within C# to power a flash object on a website. 我必须在C#中动态创建一个非常特定的形状XML文件,以便为网站上的flash对象提供动力。 The problem I have faced with many of my attempts is that most output wants each node to have some way of unique identification, I do not want that. 我在许多尝试中遇到的问题是,大多数输出都希望每个节点都有某种独特的识别方式,我不希望这样。 Rather the below is the output I am going after, not the output I currently can get. 相反,下面是我要追求的输出,而不是我目前可以获得的输出。 Note it is also invalid XML. 请注意,它也是无效的XML。
<data>
<Annual Enrollment>
<row>
<column>value1</column>
<column>value2</column>
<column>value3</column>
</row>
<row>
<column>value1</column>
<column>value2</column>
<column>value3</column>
</row>
</Annual Enrollment>
<Pre-College>
<row>
<column>value1</column>
<column>value2</column>
<column>value3</column>
</row>
<row>
<column>value1</column>
<column>value2</column>
<column>value3</column>
</row>
</Pre-College>
....AND so forth. ......等等。 The node titles or and cannot change, nor can the roots for each tree. 节点标题或者不能更改,也不能为每棵树的根目录。
The code I have so far looks like this, in my head it seems like it should work, however it does not. 到目前为止我的代码看起来像这样,在我看来它似乎应该工作,但事实并非如此。
var tableResult = DashboardData.GetMetricData(1);
// Outlinining structure
XDocument document = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement("data",
new XElement("AnnualEnrollment"),
new XElement("Pre-College"),
new XElement("Summary")
));
// Now I need to append children to each of the three nodes within the root "data"
foreach (DataRow row in tableResult.Tables[0].Rows)
{
document.Element("AnnualEnrollment").Add(new XElement("row"));
foreach (var item in row.ItemArray)
{
var element = new XElement("column", item);
}
}
Consider using XmlWriter to gain more control and flexbilily about document structure 考虑使用XmlWriter获得更多控制和灵活的文档结构
var docBuilder = new StringBuilder();
using (var writer = XmlWriter.Create(docBuilder))
{
writer.WriteStartElement("data");
writer.WriteStartElement("AnnualEnrollment");
foreach (var row in dataTable.Rows)
{
writer.WriteStartElement("row");
foreach (var item in row.ItemArray)
writer.WriteElementString("column", item);
writer.WriteEndElement(); // row
}
writer.WriteEndElement(); // Annual Enrollment
writer.WriteEndElement(); // data
}
docBuilder.Replace("<AnnualEnrollment>", "<Annual Enrollment>");
I would expect it to look more like this: 我希望它看起来更像这样:
foreach (DataRow row in tableResult.Tables[0].Rows)
{
XElement aRow = new XElement("row")
foreach (var item in row.ItemArray)
{
aRow.Add(new XElement("column", item))
}
document.Element("AnnualEnrollment").Add(aRow);
}
Sample below reads rows from first table and converts them to row elements, providing as content columns values converted to column elements. 下面的示例从第一个表读取行并将它们转换为行元素,提供转换为列元素的内容列值。
XDocument document = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement("data",
new XElement("AnnualEnrollment",
from row in tableResult.Tables[0].AsEnumerable()
select new XElement("row",
from column in tableResult.Tables[0].Columns.Cast<DataColumn>()
select new XElement("column", row[column]))),
new XElement("Pre-College"), // same for pre-college table
new XElement("Summary") // and same for summary
));
Also I'd extracted DataTable conversion into separate (extension) method: 我还将DataTable转换为单独的(扩展)方法:
public static object ToXml(this DataTable dataTable)
{
return from row in dataTable.AsEnumerable()
select new XElement("row",
from column in dataTable.Columns.Cast<DataColumn>()
select new XElement("column", row[column]));
}
Now your Xml generation will look like: 现在您的Xml生成将如下所示:
XDocument document = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement("data",
new XElement("AnnualEnrollment", tableResult.Tables[0].ToXml()),
new XElement("Pre-College", tableResult.Tables[1].ToXml()),
new XElement("Summary", tableResult.Tables[2].ToXml())
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.