From the XML Document
<?xml version="1.0" encoding="utf-8" ?>
<Data>
<Products>
<Product ProductId="1001" ProductName="ProductA" ProductPrice="123.45" />
<Product ProductId="1002" ProductName="ProductB" ProductPrice="100.45" />
</Products>
....
How to use "Sum" to find the sum of ProductPrice?
When i use
XDocument doc = XDocument.Load("Product.xml");
var sum =
from tot in (int)doc.Descendants("Product").
Attributes("ProductPrice").Sum()
select tot;
I receive Error : "can not convert type system.xml.linq.xmlattribute to int".
I'm not sure why you're using a query expression here, but I think you want:
int sum = doc.Descendants("Product")
.Sum(x => (int) x.Attribute("ProductPrice"));
The important point is that this uses the overload of Sum which lets you specify a function to go from the source element to the integer value.
Related
I have a trouble reading from XML file using LINQ.
Here is my XML file
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee>
<Name Type="First">Jack</Name>
<Name Type="Last">Black</Name>
</Employee>
<Employee>
<Name Type="First">John</Name>
<Name Type="Last">Blue</Name>
</Employee>
<Employee>
<Name Type="First">Dan</Name>
<Name Type="Last">Red</Name>
</Employee>
<Employee>
<Name Type="First">Patrick</Name>
<Name Type="Last">Green</Name>
</Employee>
</Employees>
The code I am using is following
XElement doc = XElement.Load("xmldoc.xml");
var query = from x in doc.Elements("Employee") where x.Element("Name").Attribute("Type").Value == "First" select x;
foreach (XElement item in query)
{
Console.WriteLine(item.Element("Name").Value);
}
This code returns me all first names but when i change attribute value from first to last it comes blank.
When i switch name nodes it retuns last names. For me it looks like for each employe query returns values from first name node and ignoring the second one. Could you please help me fix this?
The problem is that the x.Element("Name") call will return the first Name element. You actually need to query all the Name elements and filter for the one with the Last attribute value.
Try this instead:
var query = from x in doc.Elements("Employee").Elements("Name")
where x.Attribute("Type").Value == "Last"
select x;
foreach (XElement item in query)
{
Console.WriteLine(item.Value);
}
I want a query that will return a IEnumerable of string and inside that have
'as.m3'
'as.m4'
I have tried xDoc.Elements("moduleid") and xDoc.Descendents("moduleid")
with no luck
<?xml version="1.0" encoding="UTF-8">
<root>
<code>M11088MUBWWLSRSV9LTJBH81QT</code>
<moduleid>as.m3</moduleid>
<moduleid>as.m4</moduleid>
</root>
Use:
xDoc.Descendants("moduleid").Select(x => (string)x);
Or:
xDoc.Root.Elements("moduleid").Select(x => (string)x);
I have XML which is like:
<?xml version="1.0" encoding="utf-16"?>
<RootNodeName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" MyAttribute="7" xmlns="mylink">
<IsValid>false</IsValid>
<Name>some matrix</Name>
...Some more nodes...
</RootNodeName>
and code which is like:
var doc = XDocument.Parse(myXmlString);
Console.WriteLine(doc.Root.Element("Name"));
and console shows just an empty space since doc.Root.Element("Name") returns null =(
While I can find this Element among doc.Root.Elements() results.
doc.Root.Attribute("MyAttribute") gives correct result as well.
What is wrong with it/me?
The <Name> element is in the mylink namespace:
XNamespace mylink = "mylink";
Console.WriteLine(doc.Root.Element(mylink + "Name"));
Hi because of a misunderstanding I want to ask my question again.
I have the following XML structure:
<?xml version="1.0" encoding="utf-8"?>
<xml>
<root>
<Item>
<taxids>
<string>330</string>
<string>374</string>
<string>723</string>
<string>1087</string>
<string>1118</string>
<string>1121</string>
</taxids>
</Item>
</root>
</xml>
I need to get all the string nodes from the xml file to a string variable.
I want to get a string like this:
<taxids><string>330</string><string>374</string><string>723</string><string>1087</string><string>1118</string><string>1121</string></taxids>
My linq to xml:
var query = from ip in doc.XPathSelectElements("xml/root/Item")
select ip.XPathSelectElement("taxids").ToString();
But I am getting the following in one row of the variable query:
"System.Xml.XPath.XPathEvaluator+<EvaluateIterator>d__0`1[System.Xml.Linq.XElement]"
Is this possible?
Thanks!
Try this:
var result = doc.Element("xml")
.Element("root")
.Element("Item")
.Element("taxids")
.ToString(SaveOptions.DisableFormatting);
// result == "<taxids><string>330</string><string>374</string> ... </taxids>"
I'm trying to write an expression that will return the priority for for a filtered value. So far I've been able to get correct results when filtering for the attribute to return the value, but not the reverse. Any help with this?
//Test/FileTypes/FileType[#Priority = '10'] returns person
//Test/FileTypes/FileType/#Priority returns the three attribute values (10,11,20)
//Test/FileTypes/FileType[FileType = 'Person']/#Priority returns nothing
<?xml version="1.0" encoding="utf-8" ?>
<Test>
<FileTypes>
<FileType Priority="10">Person</FileType>
<FileType Priority="11">Job</FileType>
<FileType Priority="20">Check</FileType>
</FileTypes>
</Test>
//Test/FileTypes/FileType[FileType =
'Person']/#Priority returns nothing
As it should. A FileType doesn't have a child named FileType.
Use:
/Test/FileTypes/FileType[.= 'Person']/#Priority
Try:
//Test/FileTypes/FileType[text() = 'Person']/#Priority