1

第一次在 Stack Overflow 上发帖。

我正在努力处理 C# 中的特定 XML 结构

下面的 XML 表示表中的列及其各自的值。自然地,您可以拥有多行。

 <?xml version="1.0" encoding="UTF-16"?>
<DataTable Uid="BOY_2">
    <Columns>
        <Column Uid="Col1" Type="1" MaxLength="1"/>
        <Column Uid="DocEntry" Type="2" MaxLength="0"/>
        <Column Uid="DocNum" Type="2" MaxLength="0"/>
        <Column Uid="Doctotal" Type="11" MaxLength="0"/>
    </Columns>
    <Rows>
        <Row>
            <Cells>
                <Cell>
                    <ColumnUid>Col1</ColumnUid>
                    <Value>Y</Value>
                </Cell>
                <Cell>
                    <ColumnUid>DocEntry</ColumnUid>
                    <Value>10</Value>
                </Cell>
                <Cell>
                    <ColumnUid>DocNum</ColumnUid>
                    <Value>365</Value>
                </Cell>
                <Cell>
                    <ColumnUid>Doctotal</ColumnUid>
                    <Value>175.730000</Value>
                </Cell>
            </Cells>
        </Row>
        <Row>
            <Cells>
                <Cell>
                    <ColumnUid>Col1</ColumnUid>
                    <Value>Y</Value>
                </Cell>
                <Cell>
                    <ColumnUid>DocEntry</ColumnUid>
                    <Value>12</Value>
                </Cell>
                <Cell>
                    <ColumnUid>DocNum</ColumnUid>
                    <Value>366</Value>
                </Cell>
                <Cell>
                    <ColumnUid>Doctotal</ColumnUid>
                    <Value>173.970000</Value>
                </Cell>
            </Cells>
        </Row>
    </Rows>
</DataTable>

我正在尝试获取它,以便如果单元格下的元素称为 ColumnUid = Col1,则将下面称为值的元素中的值添加到列表中。

因此,在我的示例中,我只想从 XML 的两行中获取值“Y”。

我一生都无法弄清楚如何让它发挥作用。请帮忙!

请原谅我的措辞不佳,我是编程和处理 XML 的新手。

4

1 回答 1

0

您可以使用带有以下代码的 xml linq 读取数据表:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;
using System.IO;
namespace ConsoleApplication180
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            StreamReader reader = new StreamReader(FILENAME);
            reader.ReadLine(); //skip UTF-16
            XDocument doc = XDocument.Load(reader);

            DataTable dt = new DataTable();

            string[] columnsNames = doc.Descendants("Column").Select(x => (string)x.Attribute("Uid")).ToArray();
            DataColumn[] columns = columnsNames.Select(x => new DataColumn(x)).ToArray();
            dt.Columns.AddRange(columns);

            foreach (XElement cells in doc.Descendants("Cells"))
            {
                DataRow row = dt.Rows.Add();

                foreach (XElement cell in cells.Elements("Cell"))
                {
                    row[(string)cell.Element("ColumnUid")] = (string)cell.Element("Value");
                }

            }

        }
    }
}
于 2021-02-03T22:15:09.137 回答