Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Read XML values (attributes and elements) with C#

Read XML values (attributes and elements) with C#

Scheduled Pinned Locked Moved C#
csharpxml
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SJR_1
    wrote on last edited by
    #1

    XML File

    <Log>
    <HomeItem id="2">
    <Time>1/8/2014 3:21:47 PM</Time>
    <HasNewAddress>Y</HasNewAddress>
    <AddressType>N</AddressType>
    <Description>myDescrip</Description>
    <Item/>
    <Title/>
    </HomeItem>
    <HomeItem id="3">
    <Time>1/8/2014 6:52:47 PM</Time>
    <HasNewAddress>Y</HasNewAddress>
    <AddressType>N</AddressType>
    <Description>myDescrip2</Description>
    <Item>
    <ItemNumber id="0">
    <Name>Coffee</Name>
    <ItemDescrip>I Descrip1</ItemDescrip>
    </ItemNumber>
    <ItemNumber id="1">
    <Name>Tea</Name>
    <ItemDescrip>I Descrip 2</ItemDescrip>
    </ItemNumber>
    <ItemNumber id="2">
    <Name>Milk</Name>
    <ItemDescrip>I Descrip 3</ItemDescrip>
    </ItemNumber>
    </Item>
    <Title/>
    </HomeItem>
    <HomeItem id="4">
    <Time>1/8/2014 7:35:47 PM</Time>
    <HasNewAddress>Y</HasNewAddress>
    <AddressType>N</AddressType>
    <Description>my Descrip 3</Description>
    <Item>
    <ItemNumber id="7">
    <Name>Juice</Name>
    <ItemDescrip>I Descrip 4</ItemDescrip>
    </ItemNumber>
    <ItemNumber id="8">
    <Name>Tea</Name>
    <ItemDescrip>I Descrip 6</ItemDescrip>
    </ItemNumber>
    <ItemNumber id="9">
    <Name>Milk</Name>
    <ItemDescrip>I Descrip 7</ItemDescrip>
    </ItemNumber>
    </Item>
    <Title>The Title</Title>
    </HomeItem>
    <HomeItem id="5">
    <Time>1/8/2014 12:21:47 PM</Time>
    <HasNewAddress>Y</HasNewAddress>
    <AddressType>N</AddressType>
    <Description>myDescrip 8</Description>
    <Item/>
    <

    C 1 Reply Last reply
    0
    • S SJR_1

      XML File

      <Log>
      <HomeItem id="2">
      <Time>1/8/2014 3:21:47 PM</Time>
      <HasNewAddress>Y</HasNewAddress>
      <AddressType>N</AddressType>
      <Description>myDescrip</Description>
      <Item/>
      <Title/>
      </HomeItem>
      <HomeItem id="3">
      <Time>1/8/2014 6:52:47 PM</Time>
      <HasNewAddress>Y</HasNewAddress>
      <AddressType>N</AddressType>
      <Description>myDescrip2</Description>
      <Item>
      <ItemNumber id="0">
      <Name>Coffee</Name>
      <ItemDescrip>I Descrip1</ItemDescrip>
      </ItemNumber>
      <ItemNumber id="1">
      <Name>Tea</Name>
      <ItemDescrip>I Descrip 2</ItemDescrip>
      </ItemNumber>
      <ItemNumber id="2">
      <Name>Milk</Name>
      <ItemDescrip>I Descrip 3</ItemDescrip>
      </ItemNumber>
      </Item>
      <Title/>
      </HomeItem>
      <HomeItem id="4">
      <Time>1/8/2014 7:35:47 PM</Time>
      <HasNewAddress>Y</HasNewAddress>
      <AddressType>N</AddressType>
      <Description>my Descrip 3</Description>
      <Item>
      <ItemNumber id="7">
      <Name>Juice</Name>
      <ItemDescrip>I Descrip 4</ItemDescrip>
      </ItemNumber>
      <ItemNumber id="8">
      <Name>Tea</Name>
      <ItemDescrip>I Descrip 6</ItemDescrip>
      </ItemNumber>
      <ItemNumber id="9">
      <Name>Milk</Name>
      <ItemDescrip>I Descrip 7</ItemDescrip>
      </ItemNumber>
      </Item>
      <Title>The Title</Title>
      </HomeItem>
      <HomeItem id="5">
      <Time>1/8/2014 12:21:47 PM</Time>
      <HasNewAddress>Y</HasNewAddress>
      <AddressType>N</AddressType>
      <Description>myDescrip 8</Description>
      <Item/>
      <

      C Offline
      C Offline
      Christian Wulff
      wrote on last edited by
      #2

      I'm not 100% sure if I understand your question correctly, but I could generate your desired result with this function:

      public void Today3()
      {
      XDocument xDoc = XDocument.Load(@"C:\Results\XMLTry.xml");
      var q = from c in xDoc.Descendants("HomeItem")
      select new
      {
      Description=(string)c.Element("Description"),
      Time = (string)c.Element("Time"),
      Id=(int)c.Attribute("id")
      };

              foreach (var item in q)
                  Console.WriteLine(@"sDescrip = {0}
      

      time = {1}
      Id = {2}", item.Description, item.Time, item.Id);
      }

      Basically reading the XML elements into an anonymous type and using that to display the result.

      S 1 Reply Last reply
      0
      • C Christian Wulff

        I'm not 100% sure if I understand your question correctly, but I could generate your desired result with this function:

        public void Today3()
        {
        XDocument xDoc = XDocument.Load(@"C:\Results\XMLTry.xml");
        var q = from c in xDoc.Descendants("HomeItem")
        select new
        {
        Description=(string)c.Element("Description"),
        Time = (string)c.Element("Time"),
        Id=(int)c.Attribute("id")
        };

                foreach (var item in q)
                    Console.WriteLine(@"sDescrip = {0}
        

        time = {1}
        Id = {2}", item.Description, item.Time, item.Id);
        }

        Basically reading the XML elements into an anonymous type and using that to display the result.

        S Offline
        S Offline
        SJR_1
        wrote on last edited by
        #3

        Thank You, I have one more step to go. I'm getting the values into the variables, the only issue is getting the values that are in the item element.

        <Item>
        <ItemNumber id="0">
        <Name>Coffee</Name>
        <ItemDescrip>I Descrip1</ItemDescrip>
        </ItemNumber>
        <ItemNumber id="1">
        <Name>Tea</Name>
        <ItemDescrip>I Descrip 2</ItemDescrip>
        </ItemNumber>
        </Item>

        The tricky part to me is that sometimes they exists and other times they don't.

        C 1 Reply Last reply
        0
        • S SJR_1

          Thank You, I have one more step to go. I'm getting the values into the variables, the only issue is getting the values that are in the item element.

          <Item>
          <ItemNumber id="0">
          <Name>Coffee</Name>
          <ItemDescrip>I Descrip1</ItemDescrip>
          </ItemNumber>
          <ItemNumber id="1">
          <Name>Tea</Name>
          <ItemDescrip>I Descrip 2</ItemDescrip>
          </ItemNumber>
          </Item>

          The tricky part to me is that sometimes they exists and other times they don't.

          C Offline
          C Offline
          Christian Wulff
          wrote on last edited by
          #4

          In what kind of data structure do you need them in your program? Or do you just need to write them to the console? If so, in which format should it be written to the console? Can you give an example?

          S 1 Reply Last reply
          0
          • C Christian Wulff

            In what kind of data structure do you need them in your program? Or do you just need to write them to the console? If so, in which format should it be written to the console? Can you give an example?

            S Offline
            S Offline
            SJR_1
            wrote on last edited by
            #5

            I will be writing the records to a database. I thought the best way of doing that was getting the data into a datatable. to do that I need to add rows and populate the columns with the individual elements or attributes. The element Item and ItemNumber are only present in some conditions. So when I add them to the variables I should see something like this: +If all items exist Time = (string)c.Element("Time"), Description = (string)c.Element("Description"), ItemId = (int)c.Attribute("Itemid"), ***Note not HomeItemId**** Name = (string)c.Element("Name"), //Coffee ItemDescrip = (string)c.Element("ItemDescrip"), +If NO items exist Time = (string)c.Element("Time"), Description = (string)c.Element("Description"), ItemId = "" Name = "" ItemDescrip = "" Thanks, Sue

            C 1 Reply Last reply
            0
            • S SJR_1

              I will be writing the records to a database. I thought the best way of doing that was getting the data into a datatable. to do that I need to add rows and populate the columns with the individual elements or attributes. The element Item and ItemNumber are only present in some conditions. So when I add them to the variables I should see something like this: +If all items exist Time = (string)c.Element("Time"), Description = (string)c.Element("Description"), ItemId = (int)c.Attribute("Itemid"), ***Note not HomeItemId**** Name = (string)c.Element("Name"), //Coffee ItemDescrip = (string)c.Element("ItemDescrip"), +If NO items exist Time = (string)c.Element("Time"), Description = (string)c.Element("Description"), ItemId = "" Name = "" ItemDescrip = "" Thanks, Sue

              C Offline
              C Offline
              Christian Wulff
              wrote on last edited by
              #6

              Try that one, joining with subItems and adding a null item in the case that no subitem exists:

              public void Today3()
              {
              XDocument xDoc = XDocument.Load(@"C:\Results\XMLTry.xml");
              var q = from homeItem in xDoc.Descendants("HomeItem")
              from subItem in homeItem.Element("Item").Descendants("ItemNumber").Any () ? homeItem.Element("Item").Descendants("ItemNumber") : new List(){null}
              select new
              {
              Description=(string)homeItem.Element("Description"),
              Time = (string)homeItem.Element("Time"),
              Id=(int)homeItem.Attribute("id"),
              ItemId = subItem == null ? "" : (string)subItem.Attribute("id"),
              Name = subItem == null ? "" : (string)subItem.Element("Name"),
              ItemDescrip = subItem == null ? "" : (string)subItem.Element("ItemDescrip")
              };

                      foreach (var item in q)
                          Console.WriteLine(@"Description = {0}
              

              Time = {1}
              Id = {2}
              ItemId = {3}
              Name = {4}
              ItemDescrip = {5}
              --------------", item.Description, item.Time, item.Id, item.ItemId, item.Name, item.ItemDescrip);
              }

              S 1 Reply Last reply
              0
              • C Christian Wulff

                Try that one, joining with subItems and adding a null item in the case that no subitem exists:

                public void Today3()
                {
                XDocument xDoc = XDocument.Load(@"C:\Results\XMLTry.xml");
                var q = from homeItem in xDoc.Descendants("HomeItem")
                from subItem in homeItem.Element("Item").Descendants("ItemNumber").Any () ? homeItem.Element("Item").Descendants("ItemNumber") : new List(){null}
                select new
                {
                Description=(string)homeItem.Element("Description"),
                Time = (string)homeItem.Element("Time"),
                Id=(int)homeItem.Attribute("id"),
                ItemId = subItem == null ? "" : (string)subItem.Attribute("id"),
                Name = subItem == null ? "" : (string)subItem.Element("Name"),
                ItemDescrip = subItem == null ? "" : (string)subItem.Element("ItemDescrip")
                };

                        foreach (var item in q)
                            Console.WriteLine(@"Description = {0}
                

                Time = {1}
                Id = {2}
                ItemId = {3}
                Name = {4}
                ItemDescrip = {5}
                --------------", item.Description, item.Time, item.Id, item.ItemId, item.Name, item.ItemDescrip);
                }

                S Offline
                S Offline
                SJR_1
                wrote on last edited by
                #7

                That's exactly what I need. Thank You So Very Much! :)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups