这是要分析的XML,使用的XDocument:
<e xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FormValues />
<Others>
<Bank>
<Key>FirstKey</Key>
<Value>FirstValue</Value>
</Bank>
<Bank>
<Key>SecondKey</Key>
<Value>SecondValue</Value>
</Bank>
<Bank>
<Key>ThirdKey</Key>
<Value>ThirdValue</Value>
</Bank>
<Bank>
<Key>FourthKey</Key>
<Value>FourthValue</Value>
</Bank>
</Others>
<Prob>ProbValue</Prob>
<URL>http://example.com/</URL>
<Method>GET</Method>
</e>
如果我做:
string doc = "<e xmlns:xsd..> ..... </e>";
System.Xml.Linq.XDocument docNew = System.Xml.Linq.XDocument.Parse(doc);
var elements = docNew.Root.Descendants();
@foreach (var element in elements)
{
<label>@element.Name.ToString():</label><span>@element.Value.ToString()</span>
}
它显示:
FormValues:
Others: FirstKeyFirstValueSecondKeySecondValueThirdKeyThirdValueFourthKeyFourthValue
Bank : FirstKeyFirstValue
Key : FirstKey
Value: FirstValue
Bank : SecondKeySecondValue
Key : SecondKey
Value: SecondValue
Bank : ThirdKeyThirdValue
Key : ThirdKey
Value: ThirdValue
Bank : FourthKeyFourthValue
Key : FourthKey
Value: FourthValue
Prob : ProbValue
URL : http://example.com/
Method:GET
我只想键和值节点显示的值。 喜欢:
Others
Bank
Key : FirstKey
Value: FirstValue
Bank
Key : SecondKey
Value: SecondValue
....