Click here to Skip to main content
12,697,311 members (28,096 online)
Click here to Skip to main content
Add your own
alternative version

Stats

16.6K views
1 bookmarked
Posted

XML parsing in WP7

, 30 Jul 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Parsing and navigating XML tree in Windows Phone 7 (WP7)
Every time you need to go into a nested node use the ReadSubtree method and pass an XML string again to create a new XML reader to start from the top and skip to the next node of interest.

Use this function to process each specific node.
.... 

public void XmlToNxListingDetails(string xml) {
            NxDetailResultCollection col = new NxDetailResultCollection();
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreWhitespace = true;

            #region Get Found
            using (XmlReader xReader = XmlReader.Create(new StringReader(xml), settings)) {
                while (xReader.ReadToFollowing("found")) {
                    XMLHelper xHelp = new XMLHelper();
                    XmlNodeValue xnv = xHelp.GetNodeValue(xReader);
                    col.Found = xnv.NodeValue.ToInt();
                }
                xReader.Close();
            }
            #endregion

            using (XmlReader xReader = XmlReader.Create(new StringReader(xml), settings)) {
                while (xReader.ReadToFollowing("nxlistingdata")) {
                    NxDetailResult cData = new NxDetailResult();

                    MapNxListingData(xReader.ReadSubtree(), cData);

                    col.Add(cData);
                }
                xReader.Close();
            }

............

private void MapNxListingData(XmlReader xmlReader, NxDetailResult cData) {

            xmlReader.MoveToContent();
            xmlReader.Read();

            XMLHelper xHelp = new XMLHelper();
            NxListingData item = new NxListingData();
            item.ListingAddress = new Address();
            item.CommunityAddress = new Address();

            do {
                XmlNodeValue xnv = xHelp.GetNodeValue(xmlReader);

                switch (xnv.NodeName) {                                      
                    case "communityupdatedate": item.CommunityUpdateDate = xnv.NodeValue.ToDate(); break;
                    case "communityattributes": MapCommunityAttribute(xnv.NodeValue, item); break;
                    case "communitymedia": MapCommunityMedia(xnv.NodeValue, item); break;
                    case "agent": MapAgent(xnv.NodeValue, item); break;
                    case "office": MapOffice(xnv.NodeValue, item); break;
                    case "datasource": MapDataSource(xnv.NodeValue, item); break;
                    case "advertiser": MapAdvertiser(xnv.NodeValue, item); break;
                    case "agentadvertiser": MapAgentAdvertiser(xnv.NodeValue, item); break;
                    case "officeadvertiser": MapOfficeAdvertiser(xnv.NodeValue, item); break;
                }
            } while (!xmlReader.EOF);


            cData.AddDetailListData(item);
        }


..................


 public XmlNodeValue GetNodeValue(XmlReader inner) {
            XmlNodeValue nv = new XmlNodeValue();

            if (inner.NodeType == XmlNodeType.Element) {
                nv.NodeName = inner.LocalName;// inner.Name;
                nv.NodeValue = inner.ReadInnerXml();
            } else if (inner.NodeType == XmlNodeType.EndElement) {
                nv.NodeName = inner.LocalName;// inner.Name;
                nv.NodeValue = inner.ReadInnerXml();
            } else if (inner.NodeType == XmlNodeType.Text) {
                nv.NodeName = inner.LocalName;// inner.Name;
                nv.NodeValue = inner.Value;
                if (string.IsNullOrEmpty(nv.NodeName)) {
                    inner.Read();
                    nv.NodeName = inner.LocalName;// inner.Name;
                }
            }

            return nv;
        }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Slava Khristich
Software Developer (Senior) Tateeda Media Networks
United States United States

Software development is my passion as well as photography.


If you got a sec stop by to see my photography work at http://sk68.com


Tateeda Media Network

You may also be interested in...

Comments and Discussions

 
GeneralFormat code properly Pin
Arun Jacob29-Jul-10 4:15
memberArun Jacob29-Jul-10 4:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 30 Jul 2010
Article Copyright 2010 by Slava Khristich
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid