简体   繁体   English

innertext返回所有子文本和自身文本。 除了一个孩子的文字,我该怎么办

[英]innertext return all the child and self text. how do i except one child text

I have an xml file 我有一个xml文件

<first>
   first1
   <second>second1</second>
   first2
   <third>third1</third>
   first3
</first>

I want to read self text for <first> and child text for <third> except the child <second> 我想阅读<first>自身文本和<third>子文本,除了子<second>

answer should be 答案应该是

first1 first2 third1 first3

I tried: 我试过了:

.select(descendant::first1[not(descendant::second)]

but it's not working. 但它不起作用。 Need sug 需要建议

  XElement elem = XElement.Parse(xml);      
  var query = (from e1 in elem.Nodes()
                    where e1.GetType() == typeof(XText)
                    select (e1 as XText).Value.Trim())
                    .Union(from e2 in elem.Descendants()
                               where e2.Name.LocalName.Equals("third")
                               select e2.Value);

Try to get the the XML in XMLDocument and play with it. 尝试获取XMLDocument中的XML并使用它。

 XmlDocument doc = new XmlDocument();
  doc.LoadXml(xml);
  var nodes = doc.DocumentElement.ChildNodes;
  StringBuilder result = new StringBuilder();
  foreach (XmlNode node in nodes)
  {
    if (!node.Name.Equals("second"))
    {
      result.Append(node.InnerText);
      result.Append(" ");
    }
  }

Hope it will solve your problem. 希望它能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM