Why take just one? Linq to XML C# - c#

I can't figure out why my code just taking the first tag and not the rest.
var xml = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/Themes.xml"));
var q = from f in xml.Descendants("themes")
select new ThemesItem
{
Name = f.Element("theme").Element("name").Value,
Description = f.Element("theme").Element("description").Value,
Author = f.Element("theme").Element("author").Value,
};
return q.ToList();
ThemeItem is just a get set with public string
When i write out this data i use a repeater
Thanks for help :)

That is because the Descendants extension method takes all decendants of the xml node, that is named "themes". Since your themes node is the container for the individual theme tags, there is only one, and when you take .Element on that, you get the first occurence.
This code should work:
var q = from f in xml.Descendants("theme")
select new ThemesItem
{
Name = f.Element("name").Value,
Description = f.Element("description").Value,
Author = f.Element("author").Value,
};

<themes>
<theme>
<name>Standard</name>
<description>standard theme</description>
<author>User 1</author>
<folder>standard</folder>
</theme>
<theme>
<name>Standard</name>
<description>standard theme</description>
<author>User 2</author>
<folder>standard</folder>
</theme>
</themes>

Try using XElement.Load() instead of XDocument.Load()
http://msdn.microsoft.com/en-us/library/bb675196.aspx

Related

Linq to XML dynamic XML Decendants

I'm parsing a lot of XML files using Linq to XML synatx, everything works when I try to access top level elements
var indexroot = (from element in prodcutDocument.Root.Descendants("indexroot")
select new
{
model = (string)element.Element("MODEL"),
}).FirstOrDefault()
The problem occurs when I need to access lower level childs of that document I tried:
var indexroot = (from element in prodcutDocument.Root.Descendants("indexroot")
select new
{
ddName = (string)element.Descendants("DD_NAME").Elements("name").First();
}).FirstOrDefault()
and
var indexroot = (from element in prodcutDocument.Root.Descendants("indexroot").Descendants("DD_NAME")
select new
{
ddName = (string)element.Element("name")
}).FirstOrDefault();
Sadly none of that works and i get same error "Sequence contains no elements". And one more thing sometimes the XML document contains those tags and sometimes not is something like this enough for handling this case?
var indexroot = (from element in prodcutDocument.Root.Descendants("indexroot").Descendants("DD_NAME")
select new
{
ddName = (string)element.Element("name") ?? "-"
}).FirstOrDefault();
Edit:
I don't think is possible to paste short version of XML that would be simple, so here's full version: http://pastebin.com/uDkP3rnR and for the code example:
XDocument prodcutDocument = XDocument.Load(this.ServerPATHData + file);
var indexroot = (from element in prodcutDocument.Root.Descendants("indexroot")
select new
{
modelis = (string)element.Element("MODELIS"),
T2918_0 = (string)element.Descendants("dd_DARBINIS_GRAFIKAS_SPEC").First()
}).FirstOrDefault();
writeTxt.WriteLine("modelis: " + indexroot.modelis);
writeTxt.WriteLine("T2979_0" + indexroot.T2918_0);
In examining the sample XML that you posted on PasteBin, it appears to me that the elements that you mention appear only once. To access them, you can simply specify a path to each as follows:
XElement indexroot = document.Root.Element("indexroot");
XElement modelis = indexroot.Element("MODELIS");
XElement dd_dgs = indexroot.Element("dd_DARBINIS_GRAFIKAS_SPEC");
XElement voltageuv = dd_dgs.Element("VoltageUV");
string t2979_0 = (string)voltageuv.Element("T2979_0");
string t2861_60 = (string)voltageuv.Element("T2861_60");
string t2757_121 = (string)voltageuv.Element("T2757_121");
(Note that you may need to check for null if there is a chance that any of the elements you are trying to access may not be present. Without doing so, you'll encounter a NullReferenceException.)
Here is a snippet of the XML that you posted to give context to the above code:
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<PDB>
<indexroot>
<ed_BENDRA_MAKS_SUV_GALIA>1.45</ed_BENDRA_MAKS_SUV_GALIA>
<ed_BENDRA_MAKS_SROVE>6.48</ed_BENDRA_MAKS_SROVE>
<TIPAS>1</TIPAS>
<MODELIS>RIS 2500 HW EC 3.0</MODELIS>
<dd_DARBINIS_GRAFIKAS_SPEC>
<VoltageUV>
<T2979_0>229,42</T2979_0>
<T2861_60>227,98</T2861_60>
<T2757_121>228,97</T2757_121>
</VoltageUV>
<CurrentIA>
<T2979_0>2,56</T2979_0>
<T2861_60>2,63</T2861_60>
<T2757_121>2,72</T2757_121>
</CurrentIA>
</dd_DARBINIS_GRAFIKAS_SPEC>
</indexroot>
</PDB>
You can just change:
element.Descendants("dd_DARBINIS_GRAFIKAS_SPEC").First()
to this:
element.Descendants("dd_DARBINIS_GRAFIKAS_SPEC").FirstOrDefault() ?? "-"

XML parsing issue in WinStoreApp

i just stuck in an issue when ever i parse my XML like this using Xdocument :
XDocument xmldoc = XDocument.Load(datafromxml);
var data = from query in xmldoc.Descendants("Chapter")
select new MyEntityclass
{
Sampledata = (string)query.Element("SubChapter")
};
i got only one tag inner value from this. i.e from the first tag value only. remaining are skipped.
My XML is like :
<Chapter>
<SubChapter ChapterID="1"><![CDATA["Some data here 1"]]></SubChapter>
<SubChapter ChapterID="2"><![CDATA["Some data here 2"]]></SubChapter>
<SubChapter ChapterID="3"><![CDATA["Some data here 3"]]></SubChapter>
</Chapter>
when i checked in a debug i just got the value of "chapterid : 1". please help me to sort out this. thanks
Your query now only retreives (iterates) the outer node.
You need something like (untested)
var data = from query in xmldoc.Descendants("Chapter")
from chapter in query.Elements("SubChapter") // note the 's'
select new MyEntityclass
{
Sampledata = (string)chapter
};
var data = xmlDoc.Root
.Elements("SubChapter")
.Select(x => new MyEntityclass { Sampledata = (string)x });

Parsing xml with two identically-named elements

I try to parse this XML weather info into my application.
<weather>
<date>2014-01-03
</date>
<chanceofsnow>0</chanceofsnow>
<totalSnowfall_cm>0.0</totalSnowfall_cm>
<top>
<maxtempC>-3</maxtempC>
<maxtempF>27</maxtempF>
<mintempC>-5</mintempC>
<mintempF>24</mintempF>
</top>
<hourly>
<time>100</time>
<top>
<tempC>-6</tempC>
<tempF>20</tempF>
<windspeedMiles>8</windspeedMiles>
<windspeedKmph>13</windspeedKmph>
<winddirDegree>213</winddirDegree>
<winddir16Point>SSW</winddir16Point>
<weatherCode>113</weatherCode>
<weatherIconUrl><![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0008_clear_sky_night.png]]> </weatherIconUrl>
<weatherDesc><![CDATA[Clear]]></weatherDesc>
</top>
</hourly>
</weather>
I use the following C# to parse it:
XElement XmlSneeuw = XElement.Parse(e.Result);
//current
listBoxVandaag.ItemsSource
= from weather in XmlSneeuw.Descendants("weather")
select new AlgemeneInformatie
{
Chance_of_Snow = weather.Element("chanceofsnow").Value,
Total_Snowfall = weather.Element("totalSnowfall_cm").Value,
};
//Current
listBoxVandaagTop.ItemsSource
= from weather1 in XmlSneeuw.Descendants("top")
select new AlgemeneInformatieTop
{
Actueel_Top_maxtempC = weather1.Element("maxtempC").Value,
Actueel_Top_mintempC = weather1.Element("mintempC").Value,
};
But now there are 2 TOP elements in my xml so this wont work. what is the best way to do this? and is this the right method to parse this kind of information?
I used this site as a reference:
http://developer.nokia.com/Community/Wiki/Weather_Online_app_for_Windows_Phone
I would suggest querying the XML using LINQ and XPath, example here
//...
var topElement = XmlSneeuw.XPathSelectElement("./weather/top")
//Create your min/max object
//...
you can use .Elements("top") as below, that limit the sub level elements with same name
listBoxVandaagTop.ItemsSource = XmlSneeuw.Elements("top").Select( weather1=> new AlgemeneInformatieTop
{
Actueel_Top_maxtempC = weather1.Element("maxtempC").Value,
Actueel_Top_mintempC = weather1.Element("mintempC").Value,
});

Problems with looping over XML in C# MVC

Hi there I'm trying to parse an XML file into a list of contacts, but having problems:
public List<ContactModel> GetContacts()
{
var doc = XDocument.Load(HttpContext.Current
.Server
.MapPath(#"..\App_Data\Contacts.xml"));
var result = from items in doc.Descendants("Directory")
select new ContactModel()
{
Id = items.Attribute("ID").Value,
FirstName = items.Attribute("FirstName").Value,
LastName = items.Attribute("LastName").Value,
Telephone = items.Attribute("Telephone").Value,
Email = items.Attribute("Email").Value,
Room = items.Attribute("Room").Value,
Building = items.Attribute("Building").Value,
Location = items.Attribute("Location").Value
};
List<ContactModel> contactList = new List<ContactModel>();
foreach (var item in result)
{
contactList.Add(item);
}
return contactList;
}
I get a null exception when it's trying to loop, what am I doing wrong?
This is my XML
<?xml version="1.0" standalone="yes"?>
<ContactDirectory>
<Directory>
<ID>1</ID>
<FirstName>Peter</FirstName>
<LastName>Sutt</LastName>
<Telephone>777888</Telephone>
<Email>pett#gmail.com</Email>
<Room>3.44</Room>
<Building>Westside</Building>
<Location>Leeds</Location>
</Directory>
<Directory>
<ID>2</ID>
<FirstName>Fred</FirstName>
<LastName>West</LastName>
<Telephone>1234</Telephone>
<Email>fred#west.com</Email>
<Room>1.23</Room>
<Building>Cromwell St</Building>
<Location>Gloster</Location>
</Directory>
<Directory>
</ContactDirectory>
Beebul, They are Elements not Attributes
var contactList = (from items in doc.Descendants("Directory")
select new ContactModel()
{
Id = items.Element("ID").Value,
FirstName = items.Element("FirstName").Value,
LastName = items.Element("LastName").Value,
Telephone = items.Element("Telephone").Value,
Email = items.Element("Email").Value,
Room = items.Element("Room").Value,
Building = items.Element("Building").Value,
Location = items.Element("Location").Value
})
.ToList();
PS: you don't need to loop over your result to get a list. You can use ToList()
It looks like one or more of your attributes are missing. items.Attribute(...) returns null, and calling Value on it causes the NPE*.
Since the execution is deferred, the call does not happen until you start looping through the result.
To find the attribute that causes the problem remove all calls to Attribute(...) except for ID, verify that the crash does not happen, and start adding back the attributes one by one until the crash is back.
* After seeing the XML that you added to your question it appears that all attributes are missing! Here is a link to a short article that discusses the differences.
The Linq to XML API has implicit conversions to help with possible null references returned from .Value. Try something like (string) items.Attribute("Room").

Linq to XML retrieve single node

I have the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<game>
<name>Space Blaster</name>
<description></description>
<version>1</version>
<fullscreen>false</fullscreen>
<width>640</width>
<height>640</height>
<c2release>6900</c2release>
</game>
It's guaranteed to only have 1 game record in it. I'd like to retrieve each property value, this is what I've tried:
string ArcadeXMLLocation = Settings.GamePergatoryLocation + UserID + "/unzipped/arcade.xml";
XDocument loaded = XDocument.Load(ArcadeXMLLocation);
var q = (from c in loaded.Descendants("game") select (string)c.Element("name")).SingleOrDefault();
Response.Write(q.name);
But I can't seem to take any values like this (intellisense hates it!) Can someone show me how it's done?
I think you just need to get the value of the element:
string val = doc.Descendants("game")
.Select(x => x.Element("name").Value).FirstOrDefault();
As a prerequisite to the above, and so intellisense picks it up, make sure that you import the System.Linq and System.Xml.Linq namespaces.
This will get you all the descendants with the tag "game"
You just need the FirstOrDefault() to get the only record if it exists.
var q = from c in loaded.Descendants("game")
select new { Name = c.Element("name").Value
Description = c.Element("description").Value
};
q.FirstOrDefault();
You query is actually correct (tested and worked for me) for extracting the value of the name node - the (string) cast that you are doing will extract the value of the name node as string and not give you the node object itself, this is one of the shortcuts built into Linq to Xml. All that is left is to print out the name:
Response.Write(q);
var q = (from c in loaded.Descendants("game") select (string)c.Element("name")).SingleOrDefault();
Console.WriteLine(q);
is enough. or to avoid the cast
var q = (from c in loaded.Descendants("game") select c.Element("name").Value).SingleOrDefault();
Console.WriteLine(q);

Categories