Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to XML - Find an element

I am sure that this is basic and probably was asked before, but I am only starting using Linq to XML.

I have a simple XML that i need to read and write to.

<Documents>
...
    <Document>
      <GUID>09a1f55f-c248-44cd-9460-c0aab7c017c9-0</GUID>
      <ArchiveTime>2012-05-15T14:27:58.5270023+02:00</ArchiveTime>
      <ArchiveTimeUtc>2012-05-15T12:27:58.5270023Z</ArchiveTimeUtc>
      <IndexDatas>
        <IndexData>
          <Name>Name1</Name>
          <Value>Some value</Value>
          <DataType>1</DataType>
          <CreationTime>2012-05-15T14:27:39.6427753+02:00</CreationTime>
          <CreationTimeUtc>2012-05-15T12:27:39.6427753Z</CreationTimeUtc>
        </IndexData>
        <IndexData>
          <Name>Name2</Name>
          <Value>Some value</Value>
          <DataType>3</DataType>
          <CreationTime>2012-05-15T14:27:39.6427753+02:00</CreationTime>
          <CreationTimeUtc>2012-05-15T12:27:39.6427753Z</CreationTimeUtc>
        </IndexData>
   ...
 </IndexDatas>
</Document>
...
</Documents>

I have a "Documents" node that contains bunch of "Document" nodes.

I have GUID of the document and a "IndexData" name. I need to find the document by GUID and check if it has "IndexData" with some name. If it does not have it i need to add it.

Any help would be apreciated, as i have problem with reading and searching trough elements.

Currently I am trying to use (in C#):

IEnumerable<XElement> xmlDocuments = from c in XElement
                                        .Load(filePath)
                                        .Elements("Documents") 
                                         select c;

// fetch document
 XElement documentElementToEdit = (from c in xmlDocuments where 
                    (string)c.Element("GUID").Value == GUID select c).Single();

EDIT

xmlDocuments.Element("Documents").Elements("Document")

This returns no result, even tho xmlDocuments.Element("Documents") does. It looks like i cant get Document nodes from Documents node.

like image 237
no9 Avatar asked Jul 06 '12 06:07

no9


People also ask

What is LINQ to XML?

LINQ to XML is a LINQ-enabled, in-memory XML programming interface that enables you to work with XML from within the . NET programming languages. LINQ to XML is like the Document Object Model (DOM) in that it brings the XML document into memory.

What is Xmlnodelist?

SelectNodes(XmlNode, String) Selects a list of nodes matching the specified XPath expression. SelectNodes(XmlNode, String, XmlNamespaceManager) Selects a list of nodes matching the specified XPath expression. Any prefixes found in the XPath expression are resolved using the supplied namespace manager.

What is XElement C#?

The XElement class is one of the fundamental classes in LINQ to XML. It represents an XML element. The following list shows what you can use this class for: Create elements. Change the content of the element.

What is an XML element attribute vs element?

Attributes are part of XML elements. An element can have multiple unique attributes. Attribute gives more information about XML elements. To be more precise, they define properties of elements.


1 Answers

You can find those docs (docs without related name in index data) with below code, after that you could add your elements to the end of IndexData elements.

var relatedDocs = doc.Elements("Document")
   .Where(x=>x.Element("GUID").Value == givenValue)
   .Where(x=>!x.Element("IndexDatas")
              .Elements("IndexData")
              .Any(x=>x.Element("Name") == someValue);
like image 52
Saeed Amiri Avatar answered Oct 28 '22 01:10

Saeed Amiri