I have an XML string that looks like this:
<Attributes>
<ProductAttribute id="1">
<ProductAttributeValue>
<Value>a</Value>
</ProductAttributeValue>
</ProductAttribute>
<ProductAttribute id="2">
<ProductAttributeValue>
<Value>a</Value>
</ProductAttributeValue>
<ProductAttributeValue>
<Value>b</Value>
</ProductAttributeValue>
</ProductAttribute>
</Attributes>
I would like to return an IEnumerable like this:
Id Value
1 a
2 a b
I have tried this and only got the "b" value for Id "2":
XElement e = XElement.Parse(xmlString);
var q = from pa in e.Elements("ProductAttribute")
from pav in pa.Elements("ProductAttributeValue").Elements("Value")
select new
{
Id = (int)pa.Attribute("id"),
Value = (string)pav
};
I tried this:
XElement e = XElement.Parse(xmlString);
var q = from pa in e.Elements("ProductAttribute")
select new
{
Id = (int)pa.Attribute("id"),
Value = pa.Elements("ProductAttributeValue").Elements("Value")
};
But could not cast Value as a string. Using LINQPad the output was like this:
Id Value
1 a
2 <Value>a</Value>
<Value>b</Value>
I am trying to just return the values. Is this even possible?
Thanks.
If you wanted a contatenated string of those values like "a b"
XElement e = XElement.Parse(xmlString);
var q = from pa in e.Elements("ProductAttribute")
select new
{
Id = (int)pa.Attribute("id"),
Value = string.Join(" " ,
pa.Elements("ProductAttributeValue")
.Elements("Value")
.Select(x=>x.Value)
.ToArray())
};
XElement e = XElement.Parse(xmlString);
var q = from pa in e.Elements("ProductAttribute")
select new
{
Id = (int)pa.Attribute("id"),
Value = from pav in pa.Elements("ProductAttributeValue").Elements("Value") select pav.Value
};
Of course, Value will be an IEnumerable<string>.
Edit:
If you want the output to concat the Value elements into one string you can do this:
XElement e = XElement.Parse(xmlString);
var q = from pa in e.Elements("ProductAttribute")
select new
{
Id = (int)pa.Attribute("id"),
Value = string.Join(" ", (from pav in pa.Elements("ProductAttributeValue").Elements("Value")
select pav.Value).ToArray())
};
Then the output will be:
Id Value
1 a
2 a b
Related
I'm struggling to get the values of a single xml node using Linq.
Here is my XML.
<?xml version="1.0" encoding="utf-8"?>
<record>
<AddressLine1>abcd street</AddressLine1>
<AddressLine2>xyz AVE</AddressLine2>
<AddressCity>Illinois</AddressCity>
<AddressState>Chicago</AddressState>
<AddressZip>23434</AddressZip>
</record>
And here is my c# code
XElement xmlDoc = XElement.Parse(varQ.Content);
//When I debug I find that xmlDoc contains the XML. So that is alright.
var q = (from lpi in xmlDoc.Descendants("record")
select new { AddressLine1 = lpi.Element("AddressLine1").Value,
AddressLine2 = lpi.Element("AddressLine2").Value,
AddressCity = lpi.Element("AddressCity").Value,
AddressCountry = lpi.Element("AddressCountry").Value,
AddressState = lpi.Element("AddressState").Value,
AddressZip = lpi.Element("AddressZip").Value,
}).FirstOrDefault();
var q shows null. Can u please help me find out what is wrong here?
Replace Descendants on DescendantsAndSelf:
string xml = #"<?xml version=""1.0"" encoding=""utf-8""?>
<record>
<AddressLine1>abcd street</AddressLine1>
<AddressLine2>xyz AVE</AddressLine2>
<AddressCity>Illinois</AddressCity>
<AddressState>Chicago</AddressState>
<AddressZip>23434</AddressZip>
</record>";
XElement xmlDoc = XElement.Parse(xml);
var q = (from lpi in xmlDoc.DescendantsAndSelf("record")
select new
{
AddressLine1 = (string)lpi.Element("AddressLine1"),
AddressLine2 = (string)lpi.Element("AddressLine2"),
AddressCity = (string)lpi.Element("AddressCity"),
AddressCountry = (string)lpi.Element("AddressCountry"),
AddressState = (string)lpi.Element("AddressState"),
AddressZip = (string)lpi.Element("AddressZip"),
}).FirstOrDefault();
Console.WriteLine(q);
Print:
{ AddressLine1 = abcd street, AddressLine2 = xyz AVE, AddressCity = Illinois, AddressCountry = , AddressState = Chicago, AddressZip = 23434 }
Link: https://dotnetfiddle.net/fXQivX
I have the following XML:
<request>
<book>
<id>1833801</id>
<title>The Yiddish Policemen's Union </title>
<work>
<id>1234</id>
<name/>
</work>
<similar_books>
<book><id>243859</id><title>Stations of Tide</title> <isbn>0380817616</isbn>
<authors><author><id>14454</id><name>Michael Swanwick</name></author> </authors>
</book>
</similar_books>
<authors>
<author>
<id>2715</id><name>Michael Chabon</name>
<ratings_count>215884</ratings_count></author>
</authors>
<popular_shelves>
<shelf name="jewish" count="104"/><shelf name="sci-fi" count="100"/>
</popular_shelves>
</book>
</request>
I want to have all tags with their respective values, and I am using following code:
HttpWebRequest oReq = (HttpWebRequest)WebRequest.Create(uriRoot);
HttpWebResponse resp = (HttpWebResponse)oReq.GetResponse();
log.Info(" (ISBN= " + isbn10 + ") Http request has response.");
if (resp.ContentType.StartsWith("application/xml", StringComparison.InvariantCultureIgnoreCase))
{
Stream resultStreamISBN = resp.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); //encoding for non-latin chars
StreamReader responseReader = new StreamReader(resultStreamISBN, encode);
XDocument xdoc = XDocument.Parse(responseReader.ReadToEnd());
var books = (from u in xdoc.Descendants().Elements("book")
select new
{
id = (string)u.Element("title"),
title = (string)u.Element("title"),
works = (from i in u.Elements("work")
select new
{
work_best_book_id = (int)i.Element("id"),
work_name = (string)i.Element("name"),
}).ToList(),
authors = (from i in u.Elements("authors").Elements("author")
select new
{
id = (int)i.Element("id"),
name = (string)i.Element("name"),
rating = (int)i.Element("rating_count")
}).ToList(),
popular_shelves = (from i in u.Elements("popular_shelves").Elements("shelf")
select new
{
name = (string)i.Attribute("name"),
count = (int)i.Attribute("count")
}).ToList(),
}).ToList();
The code returns null values and is not working properly. I also should note that different xml files may not have values for all the tags.
Any suggestions on how I can improve my code?
Check for missing elements like such:
name = i.Element("name") == null ? null : i.Element("name")
For value types, you make have to change them to nullable types for this to work correctly.
You can transform
id = (int)i.Element("id"),
to
id = (int?)i.Element("id"),
Whole modified code
// I am inserting the following line to demonstrate how I load the XML in test code
// Basically, I copied & pasted xml in a file OP gave named request.xml
//XDocument xdoc = XDocument.Load( #"d:\Data\request.xml");
EDIT
XDocument xdoc = XDocument.Load("http://www.goodreads.com/book/isbn?isbn=0007295685&key=lbScLXWyNGQ1q0BDoFFSg");
var books = (from u in xdoc.Descendants().Elements("book")
select new
{
id = (string)u.Element("title"),
title = (string)u.Element("title"),
works = (from i in u.Elements("work")
select new
{
work_best_book_id = (int?)i.Element("id"),
work_name = (string)i.Element("name"),
}).ToList(),
authors = (from i in u.Elements("authors").Elements("author")
select new
{
id = (int?)i.Element("id"),
name = (string)i.Element("name"),
rating = (int?)i.Element("rating_count")
}).ToList(),
popular_shelves = (from i in u.Elements("popular_shelves").Elements("shelf")
select new
{
name = (string)i.Attribute("name"),
count = (int?)i.Attribute("count")
}).ToList(),
}).ToList();
I have xml in format below:
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<draw><drawNo>381555</drawNo>
<drawTime>2013-04-29T19:55:00+03:00</drawTime>
<result>8</result>
<result>10</result>
<result>13</result>
<result>15</result>
<result>20</result>
<result>21</result>
<result>22</result>
<result>25</result>
<result>28</result>
<result>29</result>
<result>34</result>
<result>36</result>
<result>44</result>
<result>46</result>
<result>52</result>
<result>62</result>
<result>63</result>
<result>72</result>
<result>73</result>
<result>75</result>
</draw>
I need to split the data...
I've tried the code below:
XDocument loadeddata = XDocument.Parse(e.Result);
var data = from query in loadeddata.Descendants("draw")
select new KinnoResults()
{
DrawNo = (String) query.Element("drawNo").Value,
DrawTime = (String) query.Element("drawTime").Value,
result1 = (String)query.Element("result").Value,
result2 = (String)query.Element("result").Value
};
List<KinnoResults> list = data.ToList();
But result1 and result2 hava the same value 8.
Any idea please?
var drawNo = loadeddata.Root.Element("drawNo").Value;
var drawTime = loadeddata.Root.Element("drawTime").Value;
var results = loadeddata.Descendants("result").Select(d => d.Value).ToList();
use Elements, it gets you a collection
select new KinnoResults()
{
DrawNo = (String)query.Elements("drawNo").Value,
DrawTime = (String)query.Element("drawTime").Value,
result1 = (String)query.Elements("result").ToList()[0].Value,
result2 = (String)query.Elements("result").ToList()[1].Value
};
I have this db.xml file
<items>
<item>
<title>Title1</title>
<year>2013</title>
<categories>
<category>Category1</category>
<category>Category2</category>
<category>Category3</category>
</categories>
<count>10</count>
</item>
(and so on)
</items>
I read like that:
var items = from item in xdoc.Descendants("item")
select new
{
Title = item.Element("title").Value,
Year = item.Element("year").Value,
Categories = item.Element("categories").Value, // I know this is wrong
Count = item.Element("count").Value
};
The problem is how I can read the categories and add them to list?
foreach (var item in items)
{
book.Title = item.Title;
book.Year = item.Year;
foreach (var Category in Categories)
{
book.Categories.Add(Category);
}
book.Count = item.Count;
books.Add(book);
}
It's better to use casting (to string, to int, etc then reading element's value directly. Here is query which returns integer values for Year and Count properties. Categories are IEnumerable<string>:
var items = from item in xdoc.Descendants("item")
select new {
Title = (string)item.Element("title"),
Year = (int)item.Element("year"),
Count = (int)item.Element("count"),
Categories = from c in item.Element("categories").Elements()
select (string)c
};
If you want Categories as List<string> then parse categories this way:
Categories = item.Element("categories")
.Elements()
.Select(c => (string)c)
.ToList()
You can take the list of its elements
EDITED
var items = from item in xdoc.Descendants("item")
select new
{
Title = item.Element("title").Value,
Year = item.Element("year").Value,
Categories = item.Descendants("categories").Descendants().Select(x=>x.Value).ToList(),
Count = item.Element("count").Value
};
Given the XML below how would I go about getting a list of employees who have a position in the coordinators.coordinator > position_ids?
<employee id="000001">
<username>Bernard</username>
<first_name>BERNARD</first_name>
<last_name>FISHER</last_name>
<business_phone>011111111111</business_phone>
<cell_phone>011111111112</cell_phone>
<hr_contact_name>PETER MANNING</hr_contact_name>
<contract_description>Permanent</contract_description>
<positions>
<position id="00000002" isPrimary="1">
<title>DEVELOPMENT MANAGER</title>
<department id="DV001">DEVELOPMENT</department>
<manager_position>00000002</manager_position>
<coordinators>
<coordinator position_id="00013662"/>
<coordinator position_id="00014488"/>
<coordinator position_id="00022675"/>
<coordinator position_id="00024364"/>
</coordinators>
</position>
</positions>
</employee>
<employee id="000002">
<!-- ... --->
</employee>
Here is what I have so far:
//GET EMPLOYEE WITH CORORDINATORS AND SUBORINATES WITH POSITION ID
var q = from c in d.Elements().Elements().Elements("positions").Elements("position") where (string)(c.Attribute("id")) == "028782"
select new
{
PositionID = c.Parent.Parent.Attribute("id")
,Username = c.Parent.Parent.Element("username").Value
,FirstName = c.Parent.Parent.Element("first_name").Value
,LastName = c.Parent.Parent.Element("last_name").Value
,ContractDecription = c.Parent.Parent.Element("contract_description").Value
,Title = c.Element("title").Value
,Coordinators = (from coordinator
in d.Elements().Elements().Elements("positions").Elements("position")
join p in c on coordinator.Attribute("id").Value equals p.Attribute("id").Value
select new
{
PositionID = coordinator.Attribute("id")
,Username = coordinator.Parent.Parent.Element("username").Value
,FirstName = coordinator.Parent.Parent.Element("first_name").Value
,LastName = coordinator.Parent.Parent.Element("last_name").Value
,ContractDecription = coordinator.Parent.Parent.Element("contract_description").Value
,thenode = coordinator
})
,thenode = c
};
q.Dump();
You can try something like that:
var coordinatorIds =
d.Elements("employee")
.Elements("positions")
.Elements("coordinators")
.Elements("coordinator")
.Select(c => c.Attribute("position_id").Value)
.Distinct()
.ToArray();
var coordinators =
from c in d.Elements("employee")
join id in coordinatorIds on c.Attribute("id").Value equals id
select c;