請考慮下面的示例XML。我有一個類說包含兩個字段X和Y的虛擬。通過XML解析我想建立該類的對象的列表。 X和Y分別取值於僅在B父節點內的X和Y標籤。在XML中,X和Y節點可能存在或不存在於B節點內。XML解析時,節點可能會或可能不存在
<DOC>
<A>1</A>
<B>
<C>1</C>
<D>1</D>
<E>1</E>
<X>Hello</X>
<F>1</F>
<G>1</G>
<Y>Hi</Y>
</B>
<B>
<C>1</C>
<D>1</D>
<E>1</E>
<F>1</F>
<G>1</G>
</B>
<H>
<X>1</X> //ignore
<Y>1</Y> //ignore
</H>
<DOC>
對於上面的XML,我想列出包含兩個元素的列表。第一組將有「你好」,「你好」,另一組將有「」,「」
我的C#解析代碼看起來是這樣的
List<Dummy> dummyList = new List<Dummy>();
Dummy d = null;
while (xmlReader.Read())
{
if (xmlReader.IsStartElement())
{
switch (xmlReader.Name)
{
case "B":
d = new Dummy();
while(xmlReader.Name != "X")
xmlReader.Read(); // can go into infinite loop if there is no X node
xmlReader.Read();
d.X = xmlReader.Value;
while(xmlReader.Name != "Y")
xmlReader.Read(); // can go into infinite loop if there is no Y node
xmlReader.Read();
d.Y = xmlReader.Value;
dummyList.Add(d);
d = null;
break;
}
}
}
上面代碼中的第一個B節點工作正常但不是第二個B節點。 請讓我知道你的想法。
道瓊斯指數也驗證碼失敗?請提供。 –
在您的源代碼中,您首先搜索「X」,之後搜索「Y」。你故意這樣做嗎? –