解析XML问题

时间:2014-02-25 08:24:17

标签: c# xml parsing

我正在使用for循环,SelectNodes,Attributes.GetNamedItem等解析一个巨大的XML。

我遇到了一个如何解析相同节点 CustLoyalty 的问题,这些节点与下面摘要中显示的相同。问题是如何获取相同的节点值,因为它们不仅仅位于父节点内

<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>

我的代码是这样的:

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 个答案:

答案 0 :(得分:2)

假设您使用XDocument对象解析它。请注意,如果您的输入无效,XDocument可能会抛出异常html,如果元素层次结构中顶层的xDoc中没有名称为“Customer”的元素,则xCostumer可以为null值。

XDocument xDoc = XDocument.Parse(YourStringHoldingXmlContent);
XElement xCustomer = xDoc.Element("Customer");
foreach (XElement CustLoayalty in xCustomer.Elements("CustLoyalty"))
{
    Console.WriteLine(CustomLoaylty.Value.ToString());
}

答案 1 :(得分:1)

您可以执行以下操作

1-您定义了一个CustomLoyalty类

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

2-声明一个名单,称之为uniqueCustomLoyalty

private List<CustomLoyalty> uniqueCustomLoyalty=new List<CustomLoyalty>();
当您为每个客户循环定制忠诚度时,

3-执行此操作

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

希望这会对你有所帮助

问候