【问题标题】:XDocument Descendants() displays all child values in the parent nodeXDocument Descendants() 显示父节点中的所有子值
【发布时间】:2015-02-19 09:28:16
【问题描述】:

这是要解析的 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
....

【问题讨论】:

  • 使用 Descendants() 你有 XPath 然后:.Descendants("Key or Value") 或者在这种情况下还有.Descendants(Key|Value)
  • var elementsKey = docNew.Root.Descendants("Key");然后 foreach 循环将只有键。我怎样才能包含, var elementsValue = docNew.Root.Descendants("Value");也一样?
  • 使用Descendants("Key or Value")Descendants("Key|Value")。注意or逻辑或运算符和|联合运算符。
  • 错误:|字符不能加到十六进制...

标签: c# .net xml xml-parsing


【解决方案1】:

XElement.Value 返回包含该元素所有文本内容的字符串,但您只想直接显示XText 子节点的连接值 由每个XElement 拥有。 (这些是保存元素的实际character data 的节点。)

这可以按如下方式完成:

var docNew = System.Xml.Linq.XDocument.Parse(doc);
foreach (var element in docNew.Root.Descendants())
{
    var textValue = string.Concat(element.Nodes().OfType<System.Xml.Linq.XText>().Select(tx => tx.Value));
    Console.WriteLine(string.Format("{0}: {1}", element.Name.ToString(), textValue));
}

这个逻辑可以提取成一个扩展方法:

public static partial class XNodeExtensions
{
    public static string LocalValue(this XContainer node)
    {
        if (node == null)
            return null;
        return string.Concat(node.Nodes().OfType<XText>().Select(tx => tx.Value));
    }
}

并按如下方式使用:

var textValue = element.LocalValue();

它打印以下内容:

FormValues: 
Others: 
Bank: 
Key: FirstKey
Value: FirstValue
Bank: 
Key: SecondKey
Value: SecondValue
Bank: 
Key: ThirdKey
Value: ThirdValue
Bank: 
Key: FourthKey
Value: FourthValue
Prob: ProbValue
URL: http://example.com/
Method: GET

演示小提琴here.

【讨论】:

  • 哇! XText。按预期工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-07
相关资源
最近更新 更多