Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Windows-Phone-7

XML parsing in WP7

0.00/5 (No votes)
30 Jul 2010CPOL 23.5K  
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)