I'm having problems finding the correct syntax to use.
I hope the code below will demonstrate the problem better than I can explain it in words. I am trying to retrieve an anonymous type based on matching conditions. I have to first check the Attribute for a match, and return the value only if the condition is met. If the element has an attribute named "value1" I get back the value for the attribute named "value" on the same element.
I put arrows <--- to show pseudocode where the problem is.
Thanks in advance to anyone that can help.
<item name="DataStore" >
<int name="item1" value="895"/>
<int name="item2" value="245"/>
</item>
<item name="DataStore" >
<int name="item1" value="540"/>
<int name="item2" value="97"/>
</item>
var result = from items in doc.Descendants()
where (string)items.Attribute("name") == "DataStore"
select new {
item1Value = items.Attribute("value").Value, <--- where items.Attribute("name") == "item1" ???
item2Value = items.Attribute("value").Value <--- where items.Attribute("name") == "item2" ???
}
To be more clear I need to return a "Tuple" result that has result.value1 and result.value2
So in the end I want to say something like:
foreach (var item in result) {
Console.WriteLine(item.value1); //Prints 895 on first iteration
Console.WroteLine(item.value2); //Prints 245 on first iteration
}
You need to select the child items from the items that you found that matched the DataStore and filter those based on your criteria.
var xml = #"<root><item name=""DataStore"" >
<int name=""item1"" value=""895""/>
<int name=""item2"" value=""245""/>
</item>
<item name=""DataStore"" >
<int name=""item1"" value=""540""/>
<int name=""item2"" value=""97""/>
</item></root>";
var doc = XDocument.Parse(xml);
var result = from items in doc.Descendants()
where (string)items.Attribute("name") == "DataStore"
select new
{
item1Value = (from d in items.Descendants() where d.Attributes("name").FirstOrDefault().Value.Equals("item1") select d.Attributes("value").FirstOrDefault().Value).FirstOrDefault(),
item2Value = (from d in items.Descendants() where d.Attributes("name").FirstOrDefault().Value.Equals("item2") select d.Attributes("value").FirstOrDefault().Value).FirstOrDefault()
};
foreach (var r in result)
{
Console.WriteLine($"Result {r.item1Value} {r.item2Value}");
}
Console.ReadLine();
Related
I am trying to delete one record from the XML based on some attribute value. My XML structure is given below:
<?xml version="1.0" encoding="utf-8"?>
<Users>
<User>
<UserId>12345</UserId>
<FirstName>abcd</FirstName>
<LastName>efgh</LastName>
<Email>Hello#hello.com</Email>
<IsAdmin>No</IsAdmin>
</User>
<User>
<UserId>67890</UserId>
<FirstName>ijklm</FirstName>
<LastName>nopqr</LastName>
<Email>world#world.com</Email>
<IsAdmin>No</IsAdmin>
</User>
<User>
<UserId>12678</UserId>
<FirstName>stuv</FirstName>
<LastName>wxyz</LastName>
<Email>foo#bar.com</Email>
<IsAdmin>Yes</IsAdmin>
</User>
</Users>
E.g if attribute value is 12345 it should delete the below record from the XML.
<User>
<UserId>12345</UserId>
<FirstName>abcd</FirstName>
<LastName>efgh</LastName>
<Email>Hello#hello.com</Email>
<IsAdmin>No</IsAdmin>
</User>
I tried below code but I am not able to delete anything from the XML.
string path = Server.MapPath("~/XML/users.xml");
XElement xEle = XElement.Load(path);
var qry = from element in xEle.Descendants()
where (string)element.Attribute("UserId").Value == "12345"
select element;
if (qry.Count() > 0)
qry.First().Remove();
xEle.Save(path);
I am unable to figure this out. Any help with be appreciated. Thank you
<UserId>12345</UserId> is a nested sub-element of <User>, not an attribute. See XML attribute vs XML element to see the difference.
Thus you need to do:
var userId = "12345";
var qry = from element in xEle.Descendants("User")
where (string)element.Element("UserId") == userId
select element;
var first = qry.FirstOrDefault();
if (first != null)
first.Remove();
Incidentally, by calling both Count() and First(), you are evaluating your query twice. FirstOrDefault() returns the first matching element, or null if the enumerable is empty, and is thus more efficient.
UserId is not attribute in the XML. Its an element
XElement xEle = XElement.Load("input.xml");
var qry = from element in xEle.Descendants()
where (string)element.Element("UserId") == "12345"
select element;
if (qry.Count() > 0)
qry.First().Remove();
xEle.Save("input.xml");
And in your snippet you are comparing 12345 int with string type
Im wondering how to select a specific element from the heirarchy so that I can format its text.
In the example below I would like to format the specific element to remove the time portion of the date, but I am also looking for a way to format any of the elements for example to add a currency symbol to the text between each price tag.
My example
<orders>
<order>
<type> tools </type> //I would like the ability to select this element
<text> screwdriver </text>
<id> 100981 </id>
<price> 5.00 </price>
<date> 01/01/15 12:51:36 </date>
</order>
<order>
<type> uniform </type>
<text> boots </text>
<id> 100546 </id>
<price> 25.00 </price>
<date> 12/01/15 15:30:41 </date>
</order>
</orders>
What I have so far
foreach (XElement element in doc.Descendants())
{
var nodes = element.Nodes().Where(p => p.NodeType == XmlNodeType.Text);
foreach (XText node in nodes)
{
node.Value = FirstLetterToUpper( node.Value );// set the first letter of each output to be uppercase
}
}
What I have tried
foreach (XElement element in doc.Descendants())
{
var nodes = element.Nodes().Where(p => p.NodeType == XmlNodeType.Text);
if( element.Descendants() == element.Element("date"))
{
element.Value = Convert.ToDateTime(element.Value).ToShortDateString();
}
foreach (XText node in nodes)
{
node.Value = FirstLetterToUpper( node.Value );
}
}
I have some XML experience but have never worked with XElement before.
I've been searching SO for a while now but cant find what Im looking for. The answers below are some of the suggested answers from typing this question but they dont provide a solution as the XML elements are generated dynamically in a loop.
XElement node with text node has its formatting ignored
string.Format in XElement not formatting
Any help with this would be great as I have not attempted this before. Thanks.
You can get the text node's parent using Parent property and check it's name:
foreach (XText node in doc.DescendantNodes()
.Where(x => NodeType == XmlNodeType.Text))
{
if(node.Parent.Name == "date") { ... }
if(node.Parent.Name == "price") { ... }
}
BTW, don't forget the save the document using XDocument.Save method after you made the changes.
I usually create one class to represent the xml and send yo the instance of object with correct type. for Example convert.ToDecimal(node.innerText)
I currently have the following C# with Linq to XML code working but I'm hoping there is a better way to form the Linq query to then be able to extract the results into a flat structure.
Here are the current results formatted as I would like them to appear:
7990697: result_status = Complete
7990756: result_status = Incomplete
I want to be able to access the results using the syntax result.resultStatusId instead of result.meta.ToList()[0].resultStatusId
var xDoc = XDocument.Parse(#"<fs_response status='success' timestamp='2014-07-10 14:38:39'>
<headings>
<heading for='result_status'>Result Status</heading>
<heading for='1'>First Name</heading>
<heading for='2'>Last Name</heading>
</headings>
<results>
<result id='7990697'>
<metas>
<meta id='result_status'>Complete</meta>
</metas>
<items>
<item id='1' index='3' type='text'>
<value>Bugs</value>
</item>
<item id='2' index='4' type='text'>
<value>Bunny</value>
</item>
</items>
</result>
<result id='7990756'>
<metas>
<meta id='result_status'>Incomplete</meta>
</metas>
<items>
<item id='1' index='3' type='text'>
<value>Yogi</value>
</item>
<item id='2' index='4' type='text'>
<value>Bear</value>
</item>
</items>
</result>
</results>
<total_results>2</total_results>
</fs_response>");
var results =
from x in xDoc.Elements("fs_response").Elements("results").Elements("result")
select new
{
id = x.Attribute("id").Value,
meta = from m in x.Elements("metas").Elements("meta")
where m.Attribute("id").Value == "result_status"
select new
{
resultStatusId = m.Attribute("id").Value,
resultStatus = m.Value
}
};
results.Dump();
foreach (var result in results.ToList())
{
Console.WriteLine("{0}: {1} = {2}",
result.id,
result.meta.ToList()[0].resultStatusId,
result.meta.ToList()[0].resultStatus);
//Console.WriteLine("{0}: {1} = {2}",
// result.id,
// result.resultStatusId,
// result.resultStatus);
}
I would change the query to something like this:
var results = xDoc.Element("fs_response")
.Element("results")
.Elements("result")
.Select (e => new
{
id = e.Attribute("id").Value,
meta = e.Element("metas")
.Elements("meta")
.Where (m =>
m.Attribute("id").Value == "result_status")
.Select (m => new
{
resultStatusId = m.Attribute("id").Value,
resultStatus = m.Value
})
.Single()
});
Some notes:
Notice the change in a few locations from Elements to Element. This indicates that you know there will only be one result with that name, or if you only want the first with that name. Use Elements when you expect multiple results. In some cases in the example above, I'm making assumptions about your xml schema (such as the fact that there will only be one metas Element per result). This may perform faster as it will not have to continue searching for Elements of a specific name once it has found one.
Also see the use of Single(). This indicates that you are expecting one and only one element with the id attribute equal to result_status. If you expect 0 or 1, you can use SingleOrDefault(), or if you want the first in a potential list of matching elements you can use First() or FirstOrDefault(). Once you add this, you will no longer have to call ToList()[0] in the results:
Console.WriteLine("{0}: {1} = {2}",
result.id,
result.meta.resultStatusId,
result.meta.resultStatus);
var results =
from x in xDoc.Elements("fs_response").Elements("results").Elements("result")
from m in x.Elements("metas").Elements("meta")
where m.Attribute("id").Value == "result_status"
select new
{
id = x.Attribute("id").Value,
resultStatusId = m.Attribute("id").Value,
resultStatus = m.Value,
};
foreach (var result in results.ToList())
{
Console.WriteLine("{0}: {1} = {2}", result.id, result.resultStatusId, result.resultStatus);
}
I have the following XML:
<?xml version="1.0" ?>
<NewDataSet>
<Data>
<ElementDefinition>
<ID>1</ID>
<QUANTITY>0</QUANTITY>
</ElementDefinition>
<ElementDefinition>
<ID>2</ID>
<QUANTITY>1</QUANTITY>
</ElementDefinition>
</Data>
</NewDataSet>
I need to create an array which contains all ElementDefinitions which contain a QUANTITY element with a value other then 0.
I tried:
var f = XDocument.Load(path);
var xe = f.Root.Elements("QUANTITY").Where(x => x.Value != "0").ToArray();
But that doesn't seem to work. With the above XML the array should contain 1 item, but it stays 0.
After that I need to create a string for each ElementDefinition in the array, the string must contain the value of the corresponding ID element.
For that I tried:
foreach (string x in xe)
{
string ID = //not sure what to do here
}
You want something like this:
var ids = f.Root.Descendants("ElementDefinition")
.Where(x => x.Element("QUANTITY").Value != "0")
.Select(x => x.Element("ID").Value);
As you want the ID, it is not very helpful to select all QUANTITY nodes. Instead, select exactly what you specified in your question:
All ElementDefinitions (Descendants("ElementDefinition")), that have a QUANTITY with a Value other than 0 (Where(x => x.Element("QUANTITY").Value != "0"). From the resulting nodes, select the ID (Select(x => x.Element("ID").Value)).
Yo can replace with
var xe = f.Root.Elements("Data/ElementDefinition/QUANTITY").Where(x => x.Value != "0").ToArray();
I have an XML document looking similar to this:
<items>
<item cat="1" owner="14">bla</item>
<item cat="1" owner="9">bla</item>
<item cat="1" owner="14">bla</item>
<item cat="2" owner="12">bla</item>
<item cat="2" owner="12">bla</item>
</items>
Now I'd like to get all unique owners (I actually only need the attribute value of the owner) belonging to a specified category using a linq query. In my example, the query for cat 1 would return a list containing 9 and 14. How can I do that? Linq syntax would be preferred over Lambdas. Thanks in advance ;)
Presuming the fragment is in itemsElement:
var distinctOwners = (from item in itemsElement.Element("item")
where itemElements.Attribute("cat") == 1
select item.Attribute("owner")).Distinct();
Apologies for formatting and indentation!
Try this function:-
static IEnumerable<int> GetOwners(XDocument doc, string cat)
{
return from item in doc.Descendants("item")
where item.Attribute("cat").Value == cat
select (int)item.Attribute("owner")).Distinct();
}
XElement ele = XElement.Parse(#"<items><item cat=""1"" owner=""14"">bla</item><item cat=""1"" owner=""9"">bla</item>" +
#"<item cat=""1"" owner=""14"">bla</item><item cat=""2"" owner=""12"">bla</item>" +
#"<item cat=""2"" owner=""12"">bla</item></items>");
int cat = 1;
List<int> owners = ele.Elements("item")
.Where(x=>x.Attribute("cat").Value==cat.ToString()).Select(x=>Convert.ToInt32(x.Attribute("owner").Value)).Distinct().ToList();