简体   繁体   中英

How to compare values from XML elements and sub elements in c#

I want to compare the value from the user input to the values from the XML elements and select values from another XML element. For example: Please consider the below 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> 

Now, initially, I am checking the DccCardIssueNumber provided by the user, and if it matches, I want to check whether we have a SubType for that Card. If subtype is present, I want to print DccCardIssueSubTypeFullName and if SubType is not present, I want to print just DccCardIssuerFullName .

So far I have implemented this:

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

Here, y indicates the Subtype number (1 or 2) and x indicated the DCCCardIssueNumber

Try xml linq. I used a dictionary to group cards by number

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

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM