C# Linq to XML - 查找元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11356895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linq to XML - Find an element
提问by no9
I am sure that this is basic and probably was asked before, but I am only starting using Linq to XML.
我确信这是基本的,之前可能有人问过,但我才开始使用 Linq to XML。
I have a simple XML that i need to read and write to.
我有一个需要读取和写入的简单 XML。
<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.
我有文档的 GUID 和“IndexData”名称。我需要通过 GUID 查找文档并检查它是否具有带有某个名称的“IndexData”。如果它没有它,我需要添加它。
Any help would be apreciated, as i have problem with reading and searching trough elements.
任何帮助都会受到赞赏,因为我在阅读和搜索低谷元素方面有问题。
Currently I am trying to use (in C#):
目前我正在尝试使用(在 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.
这不会返回任何结果,即使 xmlDocuments.Element("Documents") 确实如此。看起来我无法从 Documents 节点获取 Document 节点。
采纳答案by Saeed Amiri
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.
您可以使用以下代码找到这些文档(索引数据中没有相关名称的文档),然后您可以将元素添加到 IndexData 元素的末尾。
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);
回答by max
This should work:
这应该有效:
var x = XDocument.Load(filePath);
// guid in your sample xml is not a valid guid, so I changed it to a random valid one
var requiredGuid = new Guid("E61D174C-9048-438D-A532-17311F57ED9B");
var requiredName = "Name1";
var doc = x.Root
.Elements("Document")
.Where(d => (Guid)d.Element("GUID") == requiredGuid)
.FirstOrDefault();
if(doc != null)
{
var data = doc.Element("IndexDatas")
.Elements("IndexData")
.Where(d => (string)d.Element("Name") == requiredName)
.FirstOrDefault();
if(data != null)
{
// index data found
}
else
{
// index data not found
}
}
else
{
// document not found
}

