C# : Reaching a specific Xmlnode from root using XPath with nested indexes



*Pardon the formatting - my first time


I have this xmlStr defined


using System;


using System.Collections.Generic;


using System.Collections;


using System.Linq;


using System.Web;


using System.Xml;


using System.Xml.XPath;


string xmlStr = "@ <Candidate> <Content> <DOB>14-Jan-1976</DOB> <Employers> <Employer> <Name>Diane Trucking</Name> <Addresses> <Address> <AddressLine1>1st Street</AddressLine1> <AddressLine2/> <City>First City</City> <State>FL</State> <Zip>12345</Zip> </Address> <Address> <AddressLine1>1st Street</AddressLine1> <AddressLine2/> <City>Second City</City> <State>FL</State> <Zip>12346</Zip> </Address> <Address> <AddressLine1>3rd Street</AddressLine1> <AddressLine2/> <City>Third City</City> <State>FL</State> <Zip>12347</Zip> </Address> </Addresses> </Employer> <Employer> <Name>Tom Trucking</Name> <Addresses> <Address> <AddressLine1>4th Street</AddressLine1> <AddressLine2/> <City>Fourth City</City> <State>FL</State> <Zip>12348</Zip> </Address> <Address> <AddressLine1>5th Street</AddressLine1> <AddressLine2/> <City>Fifth City</City> <State>FL</State> <Zip>12349</Zip> </Address> <Address> <AddressLine1>6th Street</AddressLine1> <AddressLine2/> <City>Sixth City</City> <State>FL</State> <Zip>12340</Zip> </Address> </Addresses> </Employer> </Employers> </Content> </Candidate>";


if(xPathIt == null) {


//xPathIt.Count is also 6


}


var xmlNodes1 = xmlDoc.SelectNodes("/Candidate/Content/Employers['1']/Employer['1']/Addresses['1']/Address");


for (int i = 0; i < xmlNodes1.Count; i++ )


{


//xmlNodes1.Count = 6


}


Both xPathIt as well as xmlNodes1 return 6 "Address" elements I expected it to return 3 elements as there are 3 addresses for an employer


I wanted to target any element in the xml from root Also, If I can do this I will be able to update the specific elemnt


Any help is highly appreciated


No comments:

Post a Comment