Probably this question repeated, but i am not satiesfied with existing answers. I want to get xml element from dynamically generated xml file by attribute value. we don't know how many nodes, and its herarchy. but each element, its sub element, its sub-sub elements, sub-sub-sub elements...so on will contain unique guid as "Id" attribute :
<Element id="">
<SubElement id=""></SubElement>
<SubElement id="">
<SubSubElement id="">
<SubSubSubElement id="">
<SubSubSubSubElement id="">....other sub inside this ...</SubSubSubSubElement>
</SubSubSubElement>
</SubSubElement>
</SubElement>
</Element>
I want to find the element by only passing the Guid value. nonethless of its xpath, its node location / position. how can i do this in C#? is i need to use LINQ?
Edited:
XDocument xmldoc = XDocument.Load(xmlFilePath);
XElement selectedElement = xmldoc.Descendants().Where(x => (string) x.Attribute("id") == myIdvalue).FirstOrDefault();
Exception : "Expression cannot contain lambda expressions" I have added Using System.Linq namspaces.
SetAttribute(String, String, String)Sets the value of the attribute with the specified local name and namespace URI.
An Attribute is something that is self-contained, i.e., a color, an ID, a name. An Element is something that does or could have attributes of its own or contain other elements.
The XML attribute is a part of an XML element. The addition of attribute in XML element gives more precise properties of the element i.e, it enhances the properties of the XML element. In the above syntax element_name is the name of an element which can be any name.
This is one of the rules for an XML document to be well-formed. The root element, like all elements in XML, may have attributes.
hoipolloi has given an XPath answer, which is fine - but I would personally use LINQ to XML. (See my blog post on code and data for reasons.)
var element = parent.Descendants()
.Where(x => (Guid?) x.Attribute("id") == id)
.FirstOrDefault();
This will perform appropriate GUID parsing on each id
attribute (returning a "null" Guid?
value for non-GUIDs). If you're certain of the text format of your ID, you can cast to string instead:
var element = parent.Descendants()
.Where(x => (string) x.Attribute("id") == idText)
.FirstOrDefault();
Change the FirstOrDefault
to Single
, SingleOrDefault
or First
depending on your requirements.
EDIT: It's not at all clear what's going wrong with the code you've posted. Here's a short but complete program which shows it working fine. Please compare this with your code:
using System;
using System.Linq;
using System.Xml.Linq;
class Test
{
static void Main()
{
string xml = "<parent><foo id='bar' /><foo id='baz' /></parent>";
XDocument doc = XDocument.Parse(xml);
string idToFind = "bar";
XElement selectedElement = doc.Descendants()
.Where(x => (string) x.Attribute("id") == idToFind).FirstOrDefault();
Console.WriteLine(selectedElement);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With