使用索引器的XML元素导航,通过从父级进行索引来访问子节点. [英] XML Element Navigation using the Indexer, accessing childnodes by indexing from parents.

查看:84
本文介绍了使用索引器的XML元素导航,通过从父级进行索引来访问子节点.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套的XML文档,其示例如下:

I have a nested XML document a sample of which is as follows:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <contact>
    <adr3 />
    <city>Alicante</city>
    <time_in_spain>
      <departed_UK>1999-09-12</departed_UK>
      <residency>2010-06-21</residency>
    </time_in_spain>
    <oap>true</oap>
    <case>
      <docs_posted>2011-06-21</docs_posted>
      <completion>2011-06-21</completion>
      <payment_recieved_uk>
        <date_received>2011-06-21</date_received>
        <transfer_date>2011-06-21</transfer_date>
      </payment_recieved_uk>
    </case>
  </contact>
</root>



我想通过索引器访问诸如<date_received></date_received>之类的子节点.我正在访问诸如< adr3>之类的Contact.ChildNodes.通过contactnode ["adr3"].InnerText的方法,但是当我尝试以下任何一种方法时都无法使用:contactnode ["date_received"].InnerText或contactnode ["//date_received"].InnerText或contactnode ["case/payment_receieved_uk/date_received].InnerText.那么,我如何才能访问这些子节点.重组数据库IS和选件,但该结构以其他方式有助于管理;我宁愿仅获取正确的限定名称或通过其他方式访问.



I want to access the childnodes such as <date_received></date_received> by means of the indexer. I am accessing Contact.ChildNodes such as <adr3> by means of contactnode["adr3"].InnerText, but it will not work when I try either of: contactnode["date_received"].InnerText or contactnode["//date_received"].InnerText or contactnode["case/payment_receieved_uk/date_received"].InnerText. How, then, can I access these child nodes. Restructuring the database IS and option but the structure helps with management in other ways; I would prefer just to get the qualified name correct or access by some other means.

推荐答案

我希望此类对您有所帮助,请尝试XmlStuff.GetXmlStuffFromPath(字符串路径)为您的Xml文件.
嗯,有一种技术可以从Xml中获取所有节点.

I hope this class will help you, try XmlStuff.GetXmlStuffFromPath( string path ) for you Xml file.
Well there is the technique to get all Nodes from the Xml.

class XmlStuff
    {
        public string adr3 { get; set; }
        public string city { get; set; }
        public string departed_UK { get; set; }
        public string residency { get; set; }
        public bool oap { get; set; }
        public string docs_posted { get; set; }
        public string completion { get; set; }
        public string date_received { get; set; }
        public string transfer_date { get; set; }
        public static XmlStuff GetXmlStuffFromPath(string path)
        {
            XmlStuff xmlStuff = new XmlStuff();
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(path);
            foreach (XmlNode xmlNode in xmlDocument.ChildNodes)
            {
                switch (xmlNode.Name)
                {
                    case "root":
                        foreach (XmlNode rootChildNode in xmlNode.ChildNodes)
                        {
                            switch (rootChildNode.Name)
                            {
                                case "contact":
                                    foreach (XmlNode contactChildNode in rootChildNode.ChildNodes)
                                    {
                                        switch (contactChildNode.Name)
                                        {
                                            case "adr3":
                                                xmlStuff.adr3 = contactChildNode.InnerText;
                                                break;
                                            case "city":
                                                xmlStuff.city = contactChildNode.InnerText;
                                                break;
                                            case "time_in_spain":
                                                foreach (XmlNode timeInSpainChildNode in contactChildNode.ChildNodes)
                                                {
                                                    switch (timeInSpainChildNode.Name)
                                                    {
                                                        case "departed_UK":
                                                            xmlStuff.departed_UK = timeInSpainChildNode.InnerText;
                                                            break;
                                                        case "residency":
                                                            xmlStuff.residency = timeInSpainChildNode.InnerText;
                                                            break;
                                                        default:
                                                            break;
                                                    }
                                                }
                                                break;
                                            case "oap":
                                                xmlStuff.oap = Convert.ToBoolean(contactChildNode.InnerText);
                                                break;
                                            case "case":
                                                foreach (XmlNode caseChildNode in contactChildNode.ChildNodes)
                                                {
                                                    switch (caseChildNode.Name)
                                                    {
                                                        case "docs_posted":
                                                            xmlStuff.docs_posted = caseChildNode.InnerText;
                                                            break;
                                                        case "completion":
                                                            xmlStuff.completion = caseChildNode.InnerText;
                                                            break;
                                                        case "payment_recieved_uk":
                                                            foreach (XmlNode paymentRecievedUkChildNode in caseChildNode.ChildNodes)
                                                            {
                                                                switch (paymentRecievedUkChildNode.Name)
                                                                {
                                                                    case "date_received":
                                                                        xmlStuff.date_received = paymentRecievedUkChildNode.InnerText;
                                                                        break;
                                                                    case "transfer_date":
                                                                        xmlStuff.transfer_date = paymentRecievedUkChildNode.InnerText;
                                                                        break;
                                                                    default:
                                                                        break;
                                                                }

                                                            }
                                                            break;
                                                        default:
                                                            break;

                                                    }
                                                }
                                                break;
                                            default:
                                                break;
                                        }

                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
            return xmlStuff;
        }

    }


这篇关于使用索引器的XML元素导航,通过从父级进行索引来访问子节点.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆