PanosPlat
PanosPlat

Reputation: 958

Parse XML issue

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;
    }..........

Upvotes: 0

Views: 94

Answers (2)

Jaroslav Kadlec
Jaroslav Kadlec

Reputation: 2543

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());
}

Upvotes: 2

Monah
Monah

Reputation: 6784

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

Upvotes: 1

Related Questions