Click here to Skip to main content
15,885,782 members
Articles / Mobile Apps / Windows Phone 7
Tip/Trick

XML parsing in WP7

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
30 Jul 2010CPOL 23.1K   1   1
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.
C#
.... 

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)


Written By
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

Comments and Discussions

 
GeneralFormat code properly Pin
Arun Jacob29-Jul-10 3:15
Arun Jacob29-Jul-10 3: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.