Share via


check if an element that have Attribute with matching Value EXIST or NOT in XDocument??

Question

Wednesday, December 8, 2010 5:36 AM

How do I check if an element that have Attribute with matching Value EXIST or NOT in XDocument??

I know the following code get the value of Attribute.

Xdoc.Element("Option" ).Element("Group" ).Attribute("Name" ).Value)

 

But How do I check if element “Group”  with Attribute “name”, with value “OptMain” exist in the XDocument?

The following is the layout of the XML Document

<?xml version="1.0" encoding="utf-8"?>

<Option>

  <Group Name="OptMain">

    <Item Name="Opt1Name">Opt1Value</Item>

    <Item Name="Opt2Name">Opt2Value</Item>

    <Item Name="opt3Name">Opt3Value</Item>

  </Group>

</Option>

All replies (12)

Wednesday, December 8, 2010 5:13 PM ✅Answered

Counting to check if there exists such an attribute can be very inefficient. And you didn't use an XDocument.

bool exists = Xdoc.Element("Option").Elements("Group").Attributes("Name").Any(att => att.Value == "OptMain");

Thursday, December 9, 2010 8:47 AM ✅Answered

You need to find all the groups where there is a Name attribute with value "Value" and, if there is any, add your new element to one of them (the first, for example).

IEnumerable<XElement> groups = from g in Xdoc.Element("Option").Elements("Group")
                where g.Attributes("Name").Any(att => att.Value == "Value")
                select g;
if (groups.Any())
{
  groups.First().Add(Ixe);
}

Monday, December 13, 2010 8:53 AM ✅Answered

From the doc, find the groups with the desired Name, then from the found groups, find the items with the specific Name attribute:

IEnumerable<XElement> items = from g in newXdoc.Element("Option").Elements("Group")
        where g.Attributes("Name").Any(att => att.Value == item.Group)
        from i in g.Elements("Item")
        where i.Attributes("Name").Any(aff => aff.Value == item.Name)
        select i;
foreach (var i in items)
 Console.WriteLine("{0}", i.Value);

Wednesday, December 8, 2010 5:55 AM

I don't know if there is a direct way to do what you are trying to, but I would first get all nodes with name "Group" and loop through that node list to check if any of them has "OptMain" as attribute value.

 

I think you will have better luck here http://social.msdn.microsoft.com/Forums/en-US/xmlandnetfx/threads

 

Hope this helps :)


Wednesday, December 8, 2010 6:33 AM

I would like to suggest you LINQ to achieve this.

XmlDocument Xdoc = new XmlDocument();
      //build ur Xdoc with nodes here
      var nodes = from System.Xml.XmlNode nd in Xdoc.SelectNodes("//Option/Group")
            where nd.Attributes["name"].Value == "OptMain"
            select nd;

      if (nodes.Count() > 0)
      {
        //exists
      }
      else
      {
        //not exists
      }

 

Thanks, Srinivasa Rao Dhulipalla
Mark As Answer if it helped you.


Thursday, December 9, 2010 3:12 AM

Thank Everyone for Replying I got the code working now.


Thursday, December 9, 2010 3:20 AM

How do I add an new element in an existing element with an specific Attribute in XDocument?

for exAMPLE: add new element where Option>Group with a spcific Name ==" Value".

 

I can add new element in an existing element, but can't seem to direct it to an element with specific Attribute.

XElement Ixe = new XElement("Item", item.Value, new XAttribute("Name", item.Name));

xdoc.Element("Option").Element("Group").Add(Ixe);

 I'm suck here. how do i direct the new element to be insert into "Group element that have a specific Attribute "name" = Value ? "


Thursday, December 9, 2010 3:49 AM

Use AppendChild option.

XmlElement newElement = doc.CreateElement("Item");

newElement.SetAttribute("Name", item.Name);

doc.AppendChild(newElement);

 

 


Thursday, December 9, 2010 3:56 AM

I"m looking for the code using the XDocument class.


Thursday, December 9, 2010 4:04 AM

Maybe this article can help..

 

http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx


Friday, December 10, 2010 1:59 AM

Thank Louis. Everything work now...


Monday, December 13, 2010 7:27 AM

Louis I need your help again.  How can I retrieve the value of Nest Element.

How do I retrieve Value of Item element,    from Option element  => Group element with specific Attribute => then value from Item element with specific Attribute?

 

XDocument newXdoc = XDocument.Load(@"H:\XMLTestion.xml");
IEnumerable<XElement> groups = from g in newXdoc.Element("Option").Elements("Group")
                                where g.Attributes("Name").Any (att => att.Value == item.Group ) || g.Elements("Item").Attributes("Name").Any(aff => aff.Value    ==    item.Name)
                                select g  ;

foreach (var groupss in groups)
Console.WriteLine("{0}", groupss.Value );

 

the foreach write out all the value of each item element , but i would like the only the value of item element with an specific attribute value.