0

I am parsing a huge XML using for-loops,SelectNodes,Attributes.GetNamedItem etc.

I came across an issue of how to parse the identical nodes CustLoyalty that are identical as shown in the abstract below. The issue is how to get the identical noded values since they are not exclusively inside a parent node

<Customer>
    <PersonName>
        <NamePrefix>Ms</NamePrefix>
        <GivenName>Fra</GivenName>
        <Surname>etti</Surname>
    </PersonName>

    <Telephone FormattedInd="false" PhoneLocationType="6" PhoneNumber="10" PhoneTechType="1"/>
    <Telephone FormattedInd="false" PhoneLocationType="6" PhoneNumber="49" PhoneTechType="3"/>

    <Email DefaultInd="true" EmailType="1">z@z</Email>

    <Address Type="1">
        <AddressLine>alace</AddressLine>
        <StateProv StateCode="NY"/>
        <CountryName Code="GB"/>
    </Address>

    <CustLoyalty MembershipID="3" ProgramID="Guest"/>
    <CustLoyalty MembershipID="6" ProgramID="Freq"/>
    <CustLoyalty MembershipID="56" ProgramID="teID"/>
    <CustLoyalty MembershipID="N6" ProgramID="ID"/>
</Customer>

My code goes something like that:

XmlNodeList CustomerList = ProfileList[v].SelectNodes("df:Customer", mgr);

for (int w = 0; w < CustomerList.Count; w++)
{
    XmlNodeList PersonNameList = CustomerList[w].SelectNodes("df:PersonName", mgr);

    for (int x = 0; x < PersonNameList.Count; x++)
    {
        XmlNode NamePrefixNode = PersonNameList[x].SelectSingleNode("df:NamePrefix", mgr);
        string NamePrefix = NamePrefixNode.InnerText;

        XmlNode GivenNameNode = PersonNameList[x].SelectSingleNode("df:GivenName", mgr);
        string GivenName = GivenNameNode.InnerText;

        XmlNode SurnameNode = PersonNameList[x].SelectSingleNode("df:Surname", mgr);
        string Surname = SurnameNode.InnerText;

        myProfiles.GivenName = GivenName;
        myProfiles.Surname = Surname;
        myProfiles.NamePrefix = NamePrefix;
    }

    XmlNode TelephoneNode = CustomerList[w].SelectSingleNode("df:Telephone", mgr);

    if (TelephoneNode != null)
    {
       string PhoneNumber = TelephoneNode.Attributes.GetNamedItem("PhoneNumber").Value;

       myProfiles.Telephone = PhoneNumber;
    }..........
2
  • 7
    'I am parsing a huge XML using for-loops,SelectNodes,Attributes.GetNamedItem etc' - where is it all? Add your code to question. 'I came across an issue of how to parse the identical nodes' - what kind of issue? Exception? Add issue description to question. Commented Feb 25, 2014 at 8:25
  • I did the needed edits. Thanx for pointing it out Commented Feb 25, 2014 at 8:50

2 Answers 2

2

Let's say that you are parsing it with XDocument object. Beware that XDocument can throw exception if your input isn't valid html and element xCostumer can have null value if element with name "Customer" is not in xDoc on top level in element hierarchy.

XDocument xDoc = XDocument.Parse(YourStringHoldingXmlContent);
XElement xCustomer = xDoc.Element("Customer");
foreach (XElement CustLoayalty in xCustomer.Elements("CustLoyalty"))
{
    Console.WriteLine(CustomLoaylty.Value.ToString());
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanx but I am not using XElement approach in the rest of my parsing code
1

you can do the following

1- you define a class CustomLoyalty

public class CustomLoyalty
{
     public string Membership{get;set;}
     public string Program{get;set;}
}

2- declare a list call it uniqueCustomLoyalty

private List<CustomLoyalty> uniqueCustomLoyalty=new List<CustomLoyalty>();

3- while you are looping on the custom loyalty for each customer do this

foreach(var data in customLoyaltiesList)
{
   // customLoyaltiesList is the list of nodes of type custom loyalty
   // assume that the current object of customloyalty called detail
   CustomLoyalty detail=new CustomLoyalty(){
        Membership=data.Attributes.GetNamedItem("MembershipID").Value, // the code to get the value of membership ID according to the method you are using
        Program=data.Attributes.GetNamedItem("ProgramID").Value,
   };
   // check if the list contains the current customloyalty
   var exists=uniqueCustomLoyalty.FirstOrDefault(t=>MemberShip=detail.MemberShip && t.Program=detail.Program);
   if(exists==null) // list doesn't contain this data
        uniqueCustomLoyalty.Add(detail); // add the detail to the list to compare it with the rest of the parsing data
   else{
       // the data is not unique, you can do what ever you want
   }
}

hope this will help you

regards

1 Comment

Perfect and the comments did it very descriptive. Thanx for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.