Click here to Skip to main content
Click here to Skip to main content
Go to top

XML parsing in WP7

, 30 Jul 2010
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

Comments and Discussions

 
GeneralFormat code properly PinmemberArun Jacob29-Jul-10 3:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.8.140916.1 | Last Updated 30 Jul 2010
Article Copyright 2010 by Slava Khristich
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid