简体   繁体   English

使用C#2.0函数从XML获取属性值

[英]Getting attributes value from XML using a C# 2.0 function

I am using C# 2.0 and I have below xml format (it is just a sample) and got loaded in XPathDocument xmlData; 我正在使用C#2.0,并且具有以下xml格式(仅为示例),并加载到XPathDocument xmlData中;

<?xml version="1.0"?>
<sitedata>
<resources>
<WorldwideSites>Worldwide sites</WorldwideSites>
<PublishedDate>20120507163835</PublishedDate>
</resources>
<region code="global" title="Global">
<site defaultLanguage="en" id="tcm:0-233-1" url="/english" countryCode="" title="" order="1">
<language code="en" pubId="tcm:0-233-1" countrylang="en-GB">English</language>
</site>
</region>
<region code="NSAM" title="North &amp; South America">
<site defaultLanguage="es" id="tcm:0-520-1" url="/ar/spanish" countryCode="AR" title="Argentina">
<language code="es" pubId="tcm:0-520-1" countrylang="es-AR" >Español</language>
<language code="en" pubId="tcm:0-447-1" countrylang="en-AR" >English</language>
</site>
</region>
<region code="EU" title="Europe">
<site defaultLanguage="de" id="tcm:0-336-1" url="/at/german" countryCode="AT" title="Austria">
<language code="de" pubId="tcm:0-336-1" countrylang="de-AT" >Deutsch</language>
<language code="en" pubId="tcm:0-337-1" countrylang="en-AT" >English</language>
</site>
</region>
</sitedata>

Now I want to create a C# 2.0 function which will take this XML as input and will return back a multidimensional array or arraylist 现在,我想创建一个C#2.0函数,它将此XML作为输入并返回多维数组或arraylist

ArrayList xmldata = new ArrayList();
xmldata[0][0] will be ["233"]["en-GB"] //a middle part of pubId attribute
xmldata[1][1] will be ["520"]["es-AR"] //attribute value of countrylang
..
..
and so on

Or suggest the best approach 或建议最佳方法

Please suggest!! 请建议!

Thanks. 谢谢。

Implemented below solution for above problem. 针对以上问题实施了以下解决方案。 Please suggest for any changes. 请提出任何更改建议。

 public ArrayList GetPubIDAndCountryLangFromSitesXML(XPathDocument xPathDoc)
        {
            ArrayList retArr = new ArrayList();

            XPathNavigator navigator = xPathDoc.CreateNavigator();
            if (navigator != null)
            {
                foreach (XPathNavigator navdata in navigator.Select("sitedata/region/site/language"))
                {
                    string[] str = new string[2];
                    string pubid = navdata.SelectSingleNode("@pubId").Value;
                    string clang = navdata.SelectSingleNode("@countrylang").Value;
                    if (!string.IsNullOrEmpty(pubid) && !string.IsNullOrEmpty(clang))
                    {
                        str[0] = pubid.Split('-')[1];
                        str[1] = clang;
                        if (pubid != "481")
                        {
                            retArr.Add(str);
                        }
                    }
                }
            }
            return retArr;
        }

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

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