Read XML values (attributes and elements) with C#
-
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/>
< -
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/>
<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.
-
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.
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.
-
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.
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?
-
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?
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
-
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
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);
} -
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);
}