首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有mixed="true“的mixed=节点-如何获取innerText及其内的节点- C#

具有mixed="true“的mixed=节点-如何获取innerText及其内的节点- C#
EN

Stack Overflow用户
提问于 2019-11-01 13:41:12
回答 1查看 31关注 0票数 0

例如:

代码语言:javascript
复制
<Inflection>innerText1</Inflection>
<Inflection>innerText2<Variant>innerText2Variant</Variant></Inflection>

我们可以通过innerText1属性在C#中获取theNode.InnerText,但是如何为第2行的节点分别获取以下信息呢?

变异体

  • innerText2
  • XmlNode of
  • innerText2Variant
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-01 14:32:10

代码语言:javascript
复制
static void Main(string[] args)
    {
        string strXML = @"<root><Inflection>innerText1</Inflection>
                                <Inflection>innerText2<Variant>innerText2Variant</Variant></Inflection></root>";

        XmlDocument xml = new XmlDocument();

        xml.LoadXml(strXML);

        XmlNodeList nodesInflection = xml.SelectNodes("/root/Inflection");

        foreach(XmlNode n in nodesInflection)
        {
            if (n.ChildNodes.Count == 1)
            {
                Console.WriteLine(n.InnerText);

                //or 
                Console.WriteLine(n.ChildNodes[0].InnerText);
            }
            else
            {
                Console.WriteLine(n.ChildNodes[0].InnerText);

                XmlNode nodeVariant = n.SelectSingleNode("Variant");

                //or
                //XmlNode nodeVariant = n.ChildNodes[1];


                Console.WriteLine(nodeVariant.InnerText);

            }
        }

        Console.WriteLine("*******press any key");
        Console.ReadKey();

    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58660226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档