Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.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)的存在获取XML属性值的列表_C#_Xml_Linq - Fatal编程技术网

C# 根据子元素(LINQ to XML)的存在获取XML属性值的列表

C# 根据子元素(LINQ to XML)的存在获取XML属性值的列表,c#,xml,linq,C#,Xml,Linq,在初稿中,我没有意识到我需要标记代码的部分,因此它们被删除了,留下了不太合理的地方。固定的 下面是XML的一个片段,它使用所有属性来指定值。在主XML中,有许多元素,我刚刚在这里复制了1个。其中一些将具有元素,一些甚至不会以的形式出现 我只是很难确定是否有费用因素。我是否应该像这样循环使用l1模块s 希望这更清楚一点。再次感谢。不确定您所说的“…至少有1个元素。此示例有2个…”是什么意思,因为该示例有2个以上的元素。这应该足以让您继续: string xml = @"<L1Module m

在初稿中,我没有意识到我需要标记代码的部分,因此它们被删除了,留下了不太合理的地方。固定的

下面是XML的一个片段,它使用所有属性来指定值。在主XML中,有许多元素,我刚刚在这里复制了1个。其中一些将具有
元素,一些甚至不会以
的形式出现

我只是很难确定是否有费用因素。我是否应该像这样循环使用
l1模块
s


希望这更清楚一点。再次感谢。

不确定您所说的“…至少有1个元素。此示例有2个…”是什么意思,因为该示例有2个以上的元素。这应该足以让您继续:

string xml = @"<L1Module moduleCode=""CFINCEX-101"" 
                         moduleTypeCode=""OPTIONAL"" 
                         moduleInstanceID=""CFINCEX-101_906376"" 
                         moduleTypeDescription=""Optional"" 
                         credits=""0"" 
                         quota=""999"" 
                         placesLeft=""985"">
                   <fees>
                       <fee feesCategory=""STANDARD"" 
                            feesDescription=""Standard Category for Fees""
                            feesAmount=""10""/>
                       <fee feesCategory=""STANDARD"" 
                            feesDescription=""Standard Category for Fees""
                            feesAmount=""86""/>
                   </fees>
                   <documents/>
                   <roles/>
                   <L2Modules/>
               </L1Module>";

var document = XDocument.Parse(xml);
var l1Modules = document.Elements("L1Module")
                        .Where(el => el.HasElements);

foreach (var l1module in l1Modules)
    Console.WriteLine(l1module.Attribute("moduleCode").Value);
stringxml=@”
";
var document=XDocument.Parse(xml);
var l1Modules=document.Elements(“L1Module”)
.其中(el=>el.HasElements);
foreach(l1Modules中的l1var模块)
Console.WriteLine(l1module.Attribute(“moduleCode”).Value);

如果我理解正确,您是在试图弄清楚L1Module元素在其费用元素中是否包含费用元素?下面的代码段应该为您指明了正确的方向

        String xmlWithFees = "<L1Module moduleCode=\"CFINCEX-101\" moduleTypeCode=\"OPTIONAL\" moduleInstanceID=\"CFINCEX-101_906376\" moduleTypeDescription=\"Optional\" credits=\"0\" quota=\"999\" placesLeft=\"985\">\n" +
                        "<fees>\n" +
                        "<fee feesCategory=\"STANDARD\" feesDescription=\"Standard Category for Fees\" feesAmount=\"10\"/>\n" +
                        "<fee feesCategory=\"STANDARD\" feesDescription=\"Standard Category for Fees\" feesAmount=\"86\"/>\n" +
                        "</fees>\n" +
                        "<documents/>\n" +
                        "<roles/>\n" +
                        "<L2Modules/>\n" +
                        "</L1Module>";

        String xmlWithoutFees = "<L1Module moduleCode=\"CFINCEX-101\" moduleTypeCode=\"OPTIONAL\" moduleInstanceID=\"CFINCEX-101_906376\" moduleTypeDescription=\"Optional\" credits=\"0\" quota=\"999\" placesLeft=\"985\">\n" +
            "<fees/>\n" +
            "<documents/>\n" +
            "<roles/>\n" +
            "<L2Modules/>\n" +
            "</L1Module>";

        XDocument document = XDocument.Parse(xmlWithFees);
        var l1module = document.Elements("L1Module").Where(el => el.HasElements);
        var fees = l1module.Elements("fees");
        bool hasFees = fees.Elements("fee").Any();
        // Will print true
        Console.WriteLine("Has fees? " + hasFees);

        document = XDocument.Parse(xmlWithoutFees);
        l1module = document.Elements("L1Module").Where(el => el.HasElements);
        fees = l1module.Elements("fees");
        hasFees = fees.Elements("fee").Any();
        // Will print false
        Console.WriteLine("Has fees? " + hasFees);
String xmlWithFees=“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
"";
字符串xmlWithoutFees=“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
"";
XDocument document=XDocument.Parse(xmlWithFees);
var l1module=document.Elements(“l1module”)。其中(el=>el.HasElements);
风险值费用=L1模块要素(“费用”);
bool hasFees=fees.Elements(“fee”).Any();
//打印的是真的吗
控制台.WriteLine(“有费用吗?”+hasFees);
document=XDocument.Parse(xmlWithoutFees);
l1module=document.Elements(“l1module”)。其中(el=>el.HasElements);
费用=L1模块要素(“费用”);
hasFees=费用。要素(“费用”)。任何();
//将打印错误
控制台.WriteLine(“有费用吗?”+hasFees);

“大多数示例都涉及元素值”意味着您可以找到一些涉及属性的示例“至少有一个元素的L1模块元素”是什么意思?您的意思是有任何子XML元素吗?或者你指的是更具体的东西?啊,对不起,至少有一个费用元素的模块元素。当我用括号括起来的时候,我不知道它会被删掉。对不起,我不仅是LINQ noobie,我也是这个网站上的一员,我没有在描述中识别代码片段,它们被删除了。我重新编辑了它,希望它更有意义。
string xml = @"<L1Module moduleCode=""CFINCEX-101"" 
                         moduleTypeCode=""OPTIONAL"" 
                         moduleInstanceID=""CFINCEX-101_906376"" 
                         moduleTypeDescription=""Optional"" 
                         credits=""0"" 
                         quota=""999"" 
                         placesLeft=""985"">
                   <fees>
                       <fee feesCategory=""STANDARD"" 
                            feesDescription=""Standard Category for Fees""
                            feesAmount=""10""/>
                       <fee feesCategory=""STANDARD"" 
                            feesDescription=""Standard Category for Fees""
                            feesAmount=""86""/>
                   </fees>
                   <documents/>
                   <roles/>
                   <L2Modules/>
               </L1Module>";

var document = XDocument.Parse(xml);
var l1Modules = document.Elements("L1Module")
                        .Where(el => el.HasElements);

foreach (var l1module in l1Modules)
    Console.WriteLine(l1module.Attribute("moduleCode").Value);
        String xmlWithFees = "<L1Module moduleCode=\"CFINCEX-101\" moduleTypeCode=\"OPTIONAL\" moduleInstanceID=\"CFINCEX-101_906376\" moduleTypeDescription=\"Optional\" credits=\"0\" quota=\"999\" placesLeft=\"985\">\n" +
                        "<fees>\n" +
                        "<fee feesCategory=\"STANDARD\" feesDescription=\"Standard Category for Fees\" feesAmount=\"10\"/>\n" +
                        "<fee feesCategory=\"STANDARD\" feesDescription=\"Standard Category for Fees\" feesAmount=\"86\"/>\n" +
                        "</fees>\n" +
                        "<documents/>\n" +
                        "<roles/>\n" +
                        "<L2Modules/>\n" +
                        "</L1Module>";

        String xmlWithoutFees = "<L1Module moduleCode=\"CFINCEX-101\" moduleTypeCode=\"OPTIONAL\" moduleInstanceID=\"CFINCEX-101_906376\" moduleTypeDescription=\"Optional\" credits=\"0\" quota=\"999\" placesLeft=\"985\">\n" +
            "<fees/>\n" +
            "<documents/>\n" +
            "<roles/>\n" +
            "<L2Modules/>\n" +
            "</L1Module>";

        XDocument document = XDocument.Parse(xmlWithFees);
        var l1module = document.Elements("L1Module").Where(el => el.HasElements);
        var fees = l1module.Elements("fees");
        bool hasFees = fees.Elements("fee").Any();
        // Will print true
        Console.WriteLine("Has fees? " + hasFees);

        document = XDocument.Parse(xmlWithoutFees);
        l1module = document.Elements("L1Module").Where(el => el.HasElements);
        fees = l1module.Elements("fees");
        hasFees = fees.Elements("fee").Any();
        // Will print false
        Console.WriteLine("Has fees? " + hasFees);