I am sure this is basic and probably has been asked before, but I'm just starting to use Linq for XML.
I have simple XML that I need to read and write.
<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 a bunch of “Document” nodes.
I have a document GUID and the name is "IndexData". I need to find the document by GUID and check if it has an "IndexData" with some name. If he does not have it, I need to add it.
Any help would be appreciated since I am having trouble reading and finding proxy elements.
I'm currently 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 does not return a result, even tho xmlDocuments.Element ("Documents"). It looks like I can't get Document nodes from node documents.
source share