获取一个元素的值,但仅当其上方的元素具有特定值时
Get the value of an element, but only when the element above it has a specific value
第一次在 Stack Overflow 上发帖。
我正在努力处理 C# 中的特定 XML 结构
下面的XML表示table中的列及其各自的值。当然你可以有多行。
<?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,则将下面名为 value 的元素中的值添加到列表中。
所以在我的示例中,我只想从 XML.
的两行中获取值 'Y'
我这辈子都想不出如何让它发挥作用。请帮忙!
请原谅我措辞不佳,我是编程和处理 XML 的新手。
您可以使用 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");
}
}
}
}
}
第一次在 Stack Overflow 上发帖。
我正在努力处理 C# 中的特定 XML 结构
下面的XML表示table中的列及其各自的值。当然你可以有多行。
<?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,则将下面名为 value 的元素中的值添加到列表中。
所以在我的示例中,我只想从 XML.
的两行中获取值 'Y'我这辈子都想不出如何让它发挥作用。请帮忙!
请原谅我措辞不佳,我是编程和处理 XML 的新手。
您可以使用 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");
}
}
}
}
}