1

我必须在 C# 中动态创建一个非常特定的形状 XML 文件来为网站上的 flash 对象供电。我在许多尝试中遇到的问题是大多数输出​​都希望每个节点都有某种唯一标识方式,我不希望这样。相反,下面是我要追求的输出,而不是我目前可以获得的输出。请注意,它也是无效的 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>

....等等。节点标题或和不能改变,每棵树的根也不能改变。

到目前为止我的代码看起来像这样,在我看来它应该可以工作,但它没有。

  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);


      }
  }
4

3 回答 3

3

考虑使用 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>");
于 2012-10-31T23:27:30.910 回答
1

我希望它看起来更像这样:

  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);

  }
于 2012-10-31T23:20:38.147 回答
0

下面的示例从第一个表中读取行并将它们转换为行元素,提供转换为列元素的内容列值。

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
        ));

我还将 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]));
}

现在您的 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()) 
        ));
于 2012-10-31T23:40:04.310 回答