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;
nv.NodeValue = inner.ReadInnerXml();
} else if (inner.NodeType == XmlNodeType.EndElement) {
nv.NodeName = inner.LocalName;
nv.NodeValue = inner.ReadInnerXml();
} else if (inner.NodeType == XmlNodeType.Text) {
nv.NodeName = inner.LocalName;
nv.NodeValue = inner.Value;
if (string.IsNullOrEmpty(nv.NodeName)) {
inner.Read();
nv.NodeName = inner.LocalName;
}
}
return nv;
}