如何比较c#中XML元素和子元素的值

问题描述 投票:0回答:1

我想将用户输入的值与XML元素的值进行比较,并从另一个XML元素中选择值。例如:请考虑以下XML:

<?xml version="1.0" encoding="utf-8" ?>
<CardTypes>
  <Card>
    <DccCardIssueNumber>99</DccCardIssueNumber>
    <DccCardIssuerName>VIS</DccCardIssuerName>
    <DccCardIssuerFullName>VISHAL</DccCardIssuerFullName>
    <RPOSCardType>1600</RPOSCardType>
    <DeliveryNote>false</DeliveryNote>
  </Card>
  <Card>
    <DccCardIssueNumber>20</DccCardIssueNumber>
    <DccCardIssuerName>VIS</DccCardIssuerName>
    <DccCardIssuerFullName>VISA</DccCardIssuerFullName>
    <RPOSCardType>1600</RPOSCardType>
    <DeliveryNote>false</DeliveryNote>
    <SubType>
      <DccCardIssueSubTypeNumber>1</DccCardIssueSubTypeNumber>
      <DccCardIssueSubTypeName>VIS</DccCardIssueSubTypeName>
      <DccCardIssueSubTypeFullName>VISA</DccCardIssueSubTypeFullName>
      <RPOSCardType>1600</RPOSCardType>
      <DeliveryNote>false</DeliveryNote>
    </SubType>
    <SubType>
      <DccCardIssueSubTypeNumber>2</DccCardIssueSubTypeNumber>
      <DccCardIssueSubTypeName>DKV</DccCardIssueSubTypeName>
      <DccCardIssueSubTypeFullName>DKV</DccCardIssueSubTypeFullName>
      <RPOSCardType>2510</RPOSCardType>
      <DeliveryNote>false</DeliveryNote>
    </SubType>
  </Card>
</CardTypes> 

现在,最初,我正在检查用户提供的DccCardIssueNumber,如果匹配,则要检查我们是否有该卡的子类型。如果存在子类型,我想打印DccCardIssueSubTypeFullName,如果不存在子类型,我只想打印DccCardIssuerFullName

到目前为止,我已经实现了这个:

    using (XmlReader xr = XmlReader.Create("DccCardTypeMap.xml"))
    {
        while (xr.Read())
        {
            if (xr.IsStartElement())
            {
                switch (xr.Name.ToString())
                {
                    case "DccCardIssueNumber":
                        if (y == "")
                        {
                            if (xr.ReadString() == x)
                            {
                                xr.ReadToFollowing("DccCardIssuerFullName");
                                transaction.CardTypeName = xr.ReadString();
                            }
                        }
                        else
                        {


                            if (xr.ReadString() == x)
                            {

                                xr.ReadToFollowing("DccCardIssueSubTypeNumber");
                                if (xr.ReadString() == y)
                                {


                                    xr.ReadToFollowing("DccCardIssueSubTypeFullName");
                                    transaction.CardTypeName = xr.ReadString();
                                }

                            }
                        }
                        break;
                }
            }
        }
    }

这里,y指示子类型编号(1或2),x指示DCCCardIssueNumber

c# xml xml-parsing
1个回答
0
投票

尝试xml linq。我用字典按数字对卡片分组

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<Card> cards = doc.Descendants().Where(x => (x.Name.LocalName == "Card") || (x.Name.LocalName == "SubType"))
                .Select(x => new Card()
                {
                    DccCardIssueNumber = (string)x.Element("DccCardIssueNumber") + (string)x.Element("DccCardIssueSubTypeNumber"),
                    DccCardIssuerName = (string)x.Element("DccCardIssuerName") + (string)x.Element("DccCardIssueSubTypeName"),
                    DccCardIssuerFullName = (string)x.Element("DccCardIssuerFullName") + (string)x.Element("DccCardIssueSubTypeFullName"),
                    RPOSCardType = (string)x.Element("RPOSCardType"),
                    DeliveryNote = (string)x.Element("DeliveryNote")
                }).ToList();

            Dictionary<string, List<Card>> dict = cards.GroupBy(x => x.DccCardIssueNumber, y => y)
                .ToDictionary(x => x.Key, y => y.ToList());
        }
    }
    public class Card
    {
        public string DccCardIssueNumber { get; set; }
        public string DccCardIssuerName { get; set; }
        public string DccCardIssuerFullName { get; set; }
        public string RPOSCardType { get; set; }
        public string DeliveryNote { get; set; }

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.