简体   繁体   English

从XML文档(C#)的子节点提取InnerText失败

[英]Unsuccessfully extracting InnerText from child nodes of XML document (C#)

The XML I'm working with is as follows: 我正在使用的XML如下:

<?xml version="1.0" encoding="utf-8"?><entry_list version="1.0"><entry 
id="commode"><ew>commode</ew><subj>HH-2#CL-1#FU-2a,b,c#BD-2d</subj><art>
<artref id="commode" /><capt>commode 1</capt><dim>54,18</dim></art>
<hw>com*mode</hw><sound><wav>commod01.wav</wav><wpr>ku-!mOd</wpr></sound>
<pr>kə-ˈmōd</pr><fl>noun</fl><et>French, from <it>commode,</it> adjective, 
suitable, convenient, from Latin <it>commodus,</it> from <it>com-</it> + 
<it>modus</it> measure <ma>mete</ma></et><def><date>circa 1688</date>
<sn>1</sn><dt>:a woman's ornate cap popular in the late 17th and early 18th 
centuries</dt><sn>2 a</sn><dt>:a low chest of drawers</dt><sn>b</sn><dt>:a 
movable washstand with a cupboard underneath</dt><sn>c</sn><dt>:a boxlike 
structure holding a chamber pot under an open seat</dt><sd>also</sd><dt>:
<sx>chamber pot</sx></dt><sn>d</sn><dt>:<sx>toilet <sxn>3b</sxn></sx></dt>
</def><art><bmp>commode.bmp</bmp><cap>commode 
1</cap></art></entry></entry_list> 

The code I'm using, which I cobbled together from various related questions: 我正在使用的代码,从各种相关问题中整理而成:

System.Xml.XmlNodeList elemList = doc.GetElementsByTagName("dt");
List<string> defs = new List<string>();

for (int count = 0; count < elemList.Count; count++)
{
    string contents = string.Empty;
    foreach (System.Xml.XmlNode child in elemList[count])
    {

        if (child.NodeType == System.Xml.XmlNodeType.Element)
        {
            contents += child.InnerText;
        }
    }
    defs.Insert(count, contents);
}

The resulting List of "defs" is empty for any number of reasons, all of which are unknown to me. 出于各种原因,生成的“ defs”列表为空,而我却不知道所有这些原因。

This is using LINQ. 这是使用LINQ。 Pass "dt" for the elementName parameter. elementName参数传递“ dt”。

static List<string> GetInnerText(XDocument xDoc, string elementName)
{
    var children = from node in xDoc.Descendants(elementName).DescendantNodes()
                   where node.NodeType == XmlNodeType.Text
                   select ((XText)node).Value;
    return children.ToList();
}

I'm not sure if above is exactly what you want, so here's an alternative solution. 我不确定上面是否正是您想要的,所以这是一个替代解决方案。

static List<string> GetInnerText(XmlDocument xDoc, string elementName)
{
    List<string> innerText = new List<string>();
    var children = xDoc.GetElementsByTagName(elementName);
    foreach (XmlNode child in children)
        innerText.Add(child.InnerText);
    return innerText;
}

elemList = doc.GetElementsByTagName("dt"); returns an XmlNodeList. 返回一个XmlNodeList。 You can directly iterate this. 您可以直接对此进行迭代。

change this System.Xml.XmlNode child in elemList[count] to System.Xml.XmlNode child in elemList and look at the value of child in debugger. 将此System.Xml.XmlNode child in elemList[count]中的System.Xml.XmlNode child in elemList更改System.Xml.XmlNode child in elemList[count]中的System.Xml.XmlNode child in elemList并在调试器中查看child的值。

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

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