locked
Identifying the unqNumber and its values beneath it in adStarCar RRS feed

  • Question

  • User330911564 posted

    Here I am again 

    If you look at this xml file, 

    Inside the adStarCar there is UnqNumber. How can i use that one(UnqNumber) as an ID to be able to call the lasMute, name, email, carNumber imgBig etc in?

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

    Thursday, March 9, 2017 4:55 PM

Answers

  • User-707554951 posted

    Hi tandohtakyie.

    From your description. I make an sample. you could refer to it:

     unqNumber: <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>

    CodeBehind:

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

    Output Screenshot as below:

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 10, 2017 8:51 AM
  • User-821857111 posted

    This should get you started along the right lines. It requires C# 6 (.NET 4.6):

    var xml = XDocument.Load("C:\\test.xml"); 
    var adStarCars = xml.Root.Descendants("adStarCar").Where(e => e.Descendants("unqNumber").First().Value == "23260");
    Console.WriteLine(adStarCars.Descendants("lasMute").FirstOrDefault()?.Value);
    Console.WriteLine(adStarCars.Descendants("DealSafer").FirstOrDefault()?.Descendants("email").FirstOrDefault()?.Value);
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 10, 2017 9:03 AM

All replies

  • User-707554951 posted

    Hi tandohtakyie.

    From your description. I make an sample. you could refer to it:

     unqNumber: <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>

    CodeBehind:

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

    Output Screenshot as below:

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 10, 2017 8:51 AM
  • User-821857111 posted

    This should get you started along the right lines. It requires C# 6 (.NET 4.6):

    var xml = XDocument.Load("C:\\test.xml"); 
    var adStarCars = xml.Root.Descendants("adStarCar").Where(e => e.Descendants("unqNumber").First().Value == "23260");
    Console.WriteLine(adStarCars.Descendants("lasMute").FirstOrDefault()?.Value);
    Console.WriteLine(adStarCars.Descendants("DealSafer").FirstOrDefault()?.Descendants("email").FirstOrDefault()?.Value);
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 10, 2017 9:03 AM
  • User330911564 posted

    thank you very much 

    it works perfect.

    Friday, March 10, 2017 9:50 AM
  • User330911564 posted

    Great answer

    Friday, March 10, 2017 9:51 AM
  • User330911564 posted

    And lets say you wanna perform the same result but using classes example: 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?

    Monday, March 13, 2017 8:21 AM
  • User-821857111 posted

    If you have a new question, you should start a new thread.

    Monday, March 13, 2017 8:44 AM