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);
Related
I have an xml as an value of an element inside a Main xml. I want to scrub off or delete a node within the inner xml. How do I achieve that ?
For removing a node in main xml I am doing
var result = doc.Descendants("node1").Where(x => x.Attribute("id").Value == "002");
if (result != null)
{
result.Remove();
}
Here is my XML :
<?xml version="1.0" encoding="utf-16"?>
<root>
<node1>id="001" version="1.0"</node1>
<node2>id="002" version="1.0"</node1>
<report>raw = "<response = "10"><innerxml><prod>date = "18082016" name="pqr"</prod><seg1>id="002" name = "sqs"</seg1></innerxml></response>"</report>
</root>
Your code is correct but your xml is not. the XML should be like:
<?xml version="1.0" encoding="utf-16"?>
<root>
<node1 id="001" version="1.0"></node1>
<node2 id="002" version="1.0"></node2>
</root>
I am new to LINQ and wanted to write a C# LINQ query to filter nodes on a XDocument Object. How would I filter based on a date Value from within a tag, in this case the tag. I want to loop and create a new XDocument object with the filtered results by Date.
XDocument :
<?xml version="1.0" encoding="UTF-8"?>
<BMW>
<Model>
<Desc>335</Desc>
<ReleaseDate>6/20/2016</ReleaseDate>
<Engine>V6</Engine>
<BodyStyle></BodyStyle>
</Model>
<Model>
<Desc>550</Desc>
<ReleaseDate>7/12/2016</ReleaseDate>
<Engine>V6</Engine>
<BodyStyle></BodyStyle>
</Model>
<Model>
<Desc>750</Desc>
<ReleaseDate>8/26/2016</ReleaseDate>
<Engine>V8</Engine>
<BodyStyle>Executive Sedan</BodyStyle>
</Model>
</BMW>
Here's my method signature
private XDocument FilterByDate(XDocument xDoc, DateTime filterDate)
{
}
My Output :
If I pass the datetime value of 08/26/2016. I should essentially get back a XDocument with the the last ModelTag.
<?xml version="1.0" encoding="UTF-8"?>
<BMW>
<Model>
<Desc>750</Desc>
<ReleaseDate>8/26/2016</ReleaseDate>
<Engine>V8</Engine>
<BodyStyle>Executive Sedan</BodyStyle>
</Model>
</BMW>
This is how I would filter your model.
var model = xDoc.Descendants("Model")
.Where(m => m.Element("ReleaseDate").Value == filterDate.ToString("M/d/yyyy"))
.FirstOrDefault();
That will return an XElement. You can make your new doc with that element.
If you want a list of matches, then use .ToList() instead of .FirstOrDefault()
if you want to remove everything that's not a match, then you can negate the condition, by changing == to !=, and you add .Remove() instead of FirstOrDefault(), and of course, not assigning the "result" to a variable
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
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.