Getting attributes value from XML using a C# 2.0 function

tia_kamakshi
tia_kamakshi used Ask the Experts™
on
Hi Experts,
I am using C# 2.0 and I have below xml format (it is just a sample) and got loaded in  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>

Open in new window


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

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

Open in new window


Or suggest the best approach

Please suggest!!

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
What's exactly your problem?

Don't seem very difficult to loop through region tags, getting id attribute from site tags, splitting it through ('-') and getting countryLang attribute on language tags.

Are you having difficulties with something in particular?

Author

Commented:
@Bardobrave...thanks, can you please provide me some code sample for above problem and what should I return back .

Please suggest!!
Hi Experts,

Do i need to do any changes for the solution of above problem.

 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;
        }

Open in new window

Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

It seems fine... does it work?

Author

Commented:
@Bardobrave, it is working fine any optimization i need to do for better performance
I don't see any performance improvement that will have actual impact on a production environment.

Maybe you can seek other XML object classes and check if using them instead of XPathNavigator you get a significative impact on performance.

Author

Commented:
As suggested by expert still there were something to find in xml object for other object

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial