0

好吧,我是 C# 编程的新手,我需要一点帮助,我试图找到这个问题的答案,但找不到我想要的。也许我搜索不好,很抱歉。

我有这种类型的 xml 文件:

<ROW>
<ID>1256464</ID>
<TEST>
<PROJECT1>
<PROJECT1_ID>984461</PROJECT1_ID>
</PROJECT1>
<PROJECT2>
<PROJECT2_ID>614115</PROJECT2_ID>
</PROJECT2>
<PROJECT3>
<PROJECT3_ID>134998</PROJECT3_ID>
</PROJECT3>
</TEST>
<RESULTS>
<GRADE1>
<GRADE1_ID>6561616</GRADE1_ID>
</GRADE1>
<GRADE2>
<GRADE2_ID>6687979</GRADE2_ID>
</GRADE2>
<GRADE3>
<GRADE3_ID>13156665</GRADE3_ID>
</GRADE3>
</RESULTS>
</ROW>

有很多行,文件很大。所以我应该使用 XmlTextReader 函数。至少我写了一个小代码:

    XmlTextReader reader = new XmlTextReader("items.xml");
    while (reader.Read())
    {
    XmlNodeType nodeType = reader.NodeType;
    if (nodeType == XmlNodeType.Element)
    {
    if (reader.NodeType == "ID")
    {
    //this node value i get to the console but others not, if them are in depth
    Console.WriteLine(reader.ReadString());
    }
    //tried to do this stuff i know it's wrong
    //but i cant get the value of this elment how should i loop through
    //and of course i need to reach grade1_id fields and loop through
    if (reader.NodeType == "PROJECT2_ID")
    {
    Console.WriteLine(reader.ReadString());
    }
    }
    }
4

1 回答 1

0

我想指出您编写的 XML 无效。标签 <GRADE_something_ID> 实际上没有结束标签。(即第二个标签缺少 /-thingy)

我认为这是示例中的错字?否则,您可能会陷入解析此 XML 文件的痛苦世界。

于 2013-02-12T14:54:05.083 回答