Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用包含未知元素的LINQ to XML获取属性值_C#_Xml_Linq - Fatal编程技术网

C# 使用包含未知元素的LINQ to XML获取属性值

C# 使用包含未知元素的LINQ to XML获取属性值,c#,xml,linq,C#,Xml,Linq,作为我正在进行的项目的一部分,我需要查询XML文件并根据搜索条件查找数据。我尝试了一些使用XDocument的示例,但我的问题是XML文件上有大约7种变体需要解析。所以我不知道元素名,只知道文件可能包含哪些属性。对于我将文件连接到一个文件中的每一个变体,我的理论是它将使搜索更容易。到目前为止,这一理论被证明是错误的 “全部”将具有部分或全部属性列表。比如说 <root> <T1> <T11 name="123"/> &l

作为我正在进行的项目的一部分,我需要查询XML文件并根据搜索条件查找数据。我尝试了一些使用
XDocument
的示例,但我的问题是XML文件上有大约7种变体需要解析。所以我不知道元素名,只知道文件可能包含哪些属性。对于我将文件连接到一个文件中的每一个变体,我的理论是它将使搜索更容易。到目前为止,这一理论被证明是错误的

“全部”将具有部分或全部属性列表。比如说

<root>
    <T1>
        <T11 name="123"/>
        <H05 FileType="T52" ClientID="POB" />
    </T1>
    <T1>
        <T11 name="1234"/>
        <H05 FileType="T2" ClientID="POB" />
        <E1 ErrorCode="AA00" ErrorText="There was an Error" />
        <E1 ErrorCode="BB00" ErrorText="There was another Error" />
    </T1>
</root>


如果我想要一个搜索名称的错误集合,是否可以仅使用文件中找到的属性名称使用LINQ进行搜索?

假设您要查找文档中包含
ErrorCode
属性的所有节点:

XDocument document = LoadXml();
IEnumerable<XElement> errorNodes = document
   // Find all descendant nodes:
   .Descendants() 
    // With an "ErrorCode" attribute:
   .Where(el => el.Attribute("ErrorCode") != null);
XDocument document=LoadXml();
IEnumerable errorNodes=文档
//查找所有子代节点:
.后代()
//具有“ErrorCode”属性:
其中(el=>el.Attribute(“ErrorCode”)!=null);

假设要查找文档中包含
ErrorCode
属性的所有节点:

XDocument document = LoadXml();
IEnumerable<XElement> errorNodes = document
   // Find all descendant nodes:
   .Descendants() 
    // With an "ErrorCode" attribute:
   .Where(el => el.Attribute("ErrorCode") != null);
XDocument document=LoadXml();
IEnumerable errorNodes=文档
//查找所有子代节点:
.后代()
//具有“ErrorCode”属性:
其中(el=>el.Attribute(“ErrorCode”)!=null);

您应该能够执行类似的操作

        var xmlString = @"<root>
        <T1>
        <T11 name=""123\""/>
        <H05 FileType=""T52"" ClientID=""POB"" />
        </T1>
        <T1>
        <T11 name=""1234""/>
        <H05 FileType=""T2"" ClientID=""POB"" />
        <E1 ErrorCode=""AA00"" ErrorText=""There was an Error"" />
        <E1 ErrorCode=""BB00"" ErrorText=""There was another Error"" />
        </T1>
        </root>";

        var xml = XDocument.Parse(xmlString);

        var names = from x in xml.Descendants().Attributes("name") select x.Parent;
var xmlString=@”
";
var xml=XDocument.Parse(xmlString);
var names=从xml.subjects()中的x开始。属性(“名称”)选择x.Parent;

您应该能够执行类似的操作

        var xmlString = @"<root>
        <T1>
        <T11 name=""123\""/>
        <H05 FileType=""T52"" ClientID=""POB"" />
        </T1>
        <T1>
        <T11 name=""1234""/>
        <H05 FileType=""T2"" ClientID=""POB"" />
        <E1 ErrorCode=""AA00"" ErrorText=""There was an Error"" />
        <E1 ErrorCode=""BB00"" ErrorText=""There was another Error"" />
        </T1>
        </root>";

        var xml = XDocument.Parse(xmlString);

        var names = from x in xml.Descendants().Attributes("name") select x.Parent;
var xmlString=@”
";
var xml=XDocument.Parse(xmlString);
var names=从xml.subjects()中的x开始。属性(“名称”)选择x.Parent;

我真的不知道你在问什么。。。您能否更新您的问题,例如显示该样本XLM的期望输出?我真的不知道您在问什么。。。您是否可以更新您的问题,例如显示该示例XLM的所需输出?这看起来很像我想要的。在整个XML文档中搜索已知属性的能力。非常感谢,如果我最初的解释是不清楚的,我道歉。这看起来很像我想要的。在整个XML文档中搜索已知属性的能力。非常感谢,如果我最初的解释不清楚,我深表歉意