【问题标题】:Parse XML issue解析 XML 问题
【发布时间】:2014-02-25 08:24:16
【问题描述】:

我正在使用 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;
    }..........

【问题讨论】:

  • '我正在使用 for-loops、SelectNodes、Attributes.GetNamedItem 等解析一个巨大的 XML' - 它在哪里?将您的代码添加到问题中。 '我遇到了如何解析相同节点的问题' - 什么样的问题?例外?向问题添加问题描述。
  • 我做了必要的编辑。谢谢指出

标签: c# xml parsing


【解决方案1】:

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

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

【讨论】:

  • Thanx,但我在其余的解析代码中没有使用 XElement 方法
【解决方案2】:

您可以执行以下操作

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

希望对你有帮助

问候

【讨论】:

  • 完美,cmets 做得非常具有描述性。感谢您的帮助!
猜你喜欢
  • 2012-08-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-05
  • 2012-10-12
  • 1970-01-01
  • 2011-10-13
  • 2011-08-11
相关资源
最近更新 更多