locked
Identifying the unqNumber and its values beneath it in adStarCar with a (proxy class) class RRS feed

  • Question

  • User330911564 posted

    I have this xml and c# but i wanna have the same result using a class. public class unqnumber { get; set; } , public class name { get; set; } etc.

    and calling the class within your TextBox1 not forgetting that the unqNumber as ID that show all the other elements within the adStarCar?

    <?xml version="1.0" encoding="utf-8" ?>
    <Cars>
      <adStarCar>
        <unqNumber>23260</unqNumber>
        <lasMute>2017-02-28</lasMute>
        <DealSafer>
          <name>Gabe T</name>
          <email>gabriel@gmail.com</email>
        </DealSafer>
        <car>
          <carNumber>87-98-AE</carNumber>
          <company>Audi</company>
          <model>A8</model>
        </car>
        <BigImage>
          <imgBig>http://srv2.betterparts.org/images/audi-a8-01.jpg</imgBig>
          <imgBig>http://buyersguide.caranddriver.com/media/assets/submodel/7710.jpg</imgBig>
        </BigImage>
        <SmallImage>
          <S_imgSm>http://srv2.betterparts.org/images/audi-a8-01.jpg</S_imgSm>
          <S_imgSm>http://buyersguide.caranddriver.com/media/assets/submodel/7710.jpg</S_imgSm>
        </SmallImage>
      </adStarCar>
      <adStarCar>
        <unqNumber>8955</unqNumber>
        <lasMute>2017-01-13</lasMute>
        <DealSafer>
          <name>Richard A</name>
          <email>richard@gmail.com</email>
        </DealSafer>
        <car>
          <carNumber>78-KL-85</carNumber>
          <company>Mercedes</company>
          <model>Maybach</model>
        </car>
        <BigImage>
          <B_imgBig>http://blog.caranddriver.com/wp-content/uploads/2016/02/Mercedes-Maybach-S600-Pullman-Guard-PLACEMENT-626x382.jpg</B_imgBig>
          <B_imgBig>http://zombdrive.com/images/maybach-11.jpg</B_imgBig>
        </BigImage>
        <SmallImage>
          <S_imgSm>http://blog.caranddriver.com/wp-content/uploads/2016/02/Mercedes-Maybach-S600-Pullman-Guard-PLACEMENT-626x382.jpg</S_imgSm>
          <S_imgSm>http://zombdrive.com/images/maybach-11.jpg</S_imgSm>
        </SmallImage>
      </adStarCar>
    </Cars>

    protected void TextBox1_TextChanged(object sender, EventArgs e)
            {
                string number = TextBox1.Text;
    
                XDocument xdoc = XDocument.Load(@"C:\Users\v-guzou\Desktop\Test\Cars.txt");
                XElement adStarCar = xdoc.Descendants("adStarCar")
              .Where(s => (string)s.Element("unqNumber") == number)
              .SingleOrDefault();
                List<XElement> elementlist = new List<XElement>();
                elementlist = adStarCar.Elements().ToList();
                Response.Write(elementlist[0].Value+ "</br>");
                Response.Write(elementlist[1].Value + "</br>");
                foreach (XElement element in elementlist[2].Elements())
                {
                    Response.Write(element.Value + "</br>");
                }
                
                Response.Write(elementlist[3].Value+ "</br>");
                List<XElement> nodeelementlist = new List<XElement>();
                nodeelementlist =elementlist[4].Elements().ToList();
                Response.Write(nodeelementlist[0].Value + "</br>");
                Response.Write(nodeelementlist[1].Value + "</br>");
            }

    Monday, March 13, 2017 9:16 AM

Answers

  • User-1509636757 posted

    I understand that you want to deserialize xml to equivalent class object and then access values for specified unqNumber. If so, then here is code on similar scenario. To convert, an xml to C# class you may use this online utility to convert: Xml2CSharp.com | Convert your XML Examples into XmlSerializer compatable C# Classes

    using System;
    using System.Xml.Serialization;
    using System.Collections.Generic;
    namespace WebApp
    {
        [XmlRoot(ElementName = "DealSafer")]
        public class DealSafer
        {
            [XmlElement(ElementName = "name")]
            public string Name { get; set; }
            [XmlElement(ElementName = "email")]
            public string Email { get; set; }
        }
    
        [XmlRoot(ElementName = "car")]
        public class Car
        {
            [XmlElement(ElementName = "carNumber")]
            public string CarNumber { get; set; }
            [XmlElement(ElementName = "company")]
            public string Company { get; set; }
            [XmlElement(ElementName = "model")]
            public string Model { get; set; }
        }
    
        [XmlRoot(ElementName = "BigImage")]
        public class BigImage
        {
            [XmlElement(ElementName = "imgBig")]
            public List<string> ImgBig { get; set; }
            [XmlElement(ElementName = "B_imgBig")]
            public List<string> B_imgBig { get; set; }
        }
    
        [XmlRoot(ElementName = "SmallImage")]
        public class SmallImage
        {
            [XmlElement(ElementName = "S_imgSm")]
            public List<string> S_imgSm { get; set; }
        }
    
        [XmlRoot(ElementName = "adStarCar")]
        public class AdStarCar
        {
            [XmlElement(ElementName = "unqNumber")]
            public string UnqNumber { get; set; }
            [XmlElement(ElementName = "lasMute")]
            public string LasMute { get; set; }
            [XmlElement(ElementName = "DealSafer")]
            public DealSafer DealSafer { get; set; }
            [XmlElement(ElementName = "car")]
            public Car Car { get; set; }
            [XmlElement(ElementName = "BigImage")]
            public BigImage BigImage { get; set; }
            [XmlElement(ElementName = "SmallImage")]
            public SmallImage SmallImage { get; set; }
        }
    
        [XmlRoot(ElementName = "Cars")]
        public class Cars
        {
            [XmlElement(ElementName = "adStarCar")]
            public List<AdStarCar> AdStarCar { get; set; }
        }
    
    }
    

    Here is how you can deserialize and find values for unqNumber:

    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //-- deserialize xml file to class object
            XmlSerializer serializer = new XmlSerializer(typeof(Cars));
            Cars resultObject = (Cars)serializer.Deserialize(new XmlTextReader(Server.MapPath("~/XmlFile.xml")));
    
            //-- get the object of perticular unqNumber
            string number = "8955";
            AdStarCar carObj = resultObject.AdStarCar.Where(rec => rec.UnqNumber == number).FirstOrDefault();
    
            //-- access values now:
            if (null != carObj)
            {
                //carObj.Car.CarNumber
                //carObj.Car.Company
                //carObj.Car.Model
            }
        }
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 13, 2017 12:03 PM