0

我有一个如下的 XML 文档:

<Registrations>
  <RegistrationForm>
    <RegValue Id="Passport" v="13.999.567" />
    <RegValue Id="FavoriteColor" v="Blue" />
    <RegValue Id="Gender" v="Male" />
  </RegistrationForm>
  <RegistrationForm>
    <RegValue Id="Passport" v="12.566.342" />
    <RegValue Id="FavoriteColor" v="Red" />
    <RegValue Id="Gender" v="Female" />
  </RegistrationForm>
</Registrations>

目标是生成具有不同 ID 值作为列的 GridView,并且每一行将包含该 ID 的值:

PASSPORT   | FavoriteColor | Gender
13.999.567 | Blue          | Male
12.566.342 | Red           | Female

这里的复杂性在于我们可以拥有更多带有我事先不知道的 ID 的 RegValue 元素。所以列应该是动态生成的。例如,我可以在 XML 中添加一个元素:<RegValue Id="Pet" v="Dog" />所以表上应该有一个新的“宠物”列。

我开始使用 C# LINQ to XML,但我只能使用硬编码的列名执行查询。

4

2 回答 2

1

By using XML to DataTable with LINQ you can display XML in GridView. Hope below example may help you to solve your question.

public void ParseXMLToDataTable()
    {
        XElement document = XElement.Parse(
       @"
            <Registrations>
              <RegistrationForm>
                <RegValue Id=""Passport"" v=""13.999.567"" />
                <RegValue Id=""FavoriteColor"" v=""Blue"" />
                <RegValue Id=""Gender"" v=""Male"" />
              </RegistrationForm>
              <RegistrationForm>
                <RegValue Id=""Passport"" v=""12.566.342"" />
                <RegValue Id=""FavoriteColor"" v=""Red"" />
                <RegValue Id=""Gender"" v=""Female"" />
              </RegistrationForm>
            </Registrations>
        ");

        List<XElement> RegistrationForm = document.Elements("RegistrationForm").ToList();

        if (RegistrationForm.Count > 0)
        {
           DataTable oGridViewTable =  XElementToDataTable(RegistrationForm);
        }
    }




public DataTable XElementToDataTable(List<XElement> oRegistrationFormList)
    {
        DataTable dt = new DataTable();

        // Generate DataTable Column
        XElement oFirstElement = oRegistrationFormList.First();

        dt.Columns.AddRange(oFirstElement.Descendants().Select(o =>
           new DataColumn(o.Attribute("Id").Value)).ToArray());


        //Generate DataTable Rows
        foreach (XElement oRegistrationForm in oRegistrationFormList)
        {
            DataRow dr = dt.NewRow();
            foreach (XElement oRegValue in oRegistrationForm.Descendants())
                dr[oRegValue.Attribute("Id").Value] = oRegValue.Attribute("v").Value; 
            dt.Rows.Add(dr);
        }

        return dt;
    }
于 2012-09-08T05:22:07.420 回答
0

在我看来,您应该根据您的问题进行定制。像这样:

using System.Data;
using System.Xml.Linq;
using System.Linq;

class Program
{

    public static void Main()
    {
        XDocument document = XDocument.Parse(
            @"
                <Registrations>
                  <RegistrationForm>
                    <RegValue Id=""Passport"" v=""13.999.567"" />
                    <RegValue Id=""FavoriteColor"" v=""Blue"" />
                    <RegValue Id=""Gender"" v=""Male"" />
                  </RegistrationForm>
                  <RegistrationForm>
                    <RegValue Id=""Passport"" v=""12.566.342"" />
                    <RegValue Id=""FavoriteColor"" v=""Red"" />
                    <RegValue Id=""Gender"" v=""Female"" />
                  </RegistrationForm>
                </Registrations>
            "
        );

        DataTable table = new DataTable();

        XElement firstElement = document
                                .Root
                                .Elements(
                                    "Registrations"
                                )
                                .Elements(
                                    "RegistrationForm"
                                )
                                .FirstOrDefault();


        if (firstElement == null)

            return;

        table
        .Columns
        .AddRange(
            firstElement
            .Elements(
                "RegValue"
            )
            .Select(
                e =>
                new DataColumn(e.Attribute("Id").Value)
            )
            .ToArray()
        );

        table
        .Rows
        .Add(
            document
            .Root
            .Elements(
                "Registrations"
            )
            .Elements(
                "RegistrationForm"
            )
            .Select(
                e =>
                e
                .Elements()
                .Select(
                    regValue =>
                    regValue
                    .Attribute("v")
                    .Value
                )
                .ToArray()
            )
        );

        //YOUR TABLE IS READY HERE.
    }
}
于 2012-09-08T04:25:34.180 回答