如何使用LINQ基于特定节点的属性搜索来打印元素的innertext?

时间:2016-03-12 17:35:49

标签: c# xml linq linq-to-xml

**我有这样的XML -

$.components.register("dataTable", {
  defaults: {
    responsive: true,
    language: {
      "sSearchPlaceholder": "Search..",
      "lengthMenu": "_MENU_",
      "search": "_INPUT_",
      "paginate": {
        "previous": '<i class="icon wb-chevron-left-mini"></i>',
        "next": '<i class="icon wb-chevron-right-mini"></i>'
      }
    }
  },
  api: function() {
    if (!$.fn.dataTable) return;

    if ($.fn.dataTable.TableTools) {
      // Set the classes that TableTools uses to something suitable for Bootstrap
      $.extend(true, $.fn.dataTable.TableTools.classes, {
        "container": "DTTT btn-group btn-group pull-left",
        "buttons": {
          "normal": "btn btn-outline btn-default",
          "disabled": "disabled"
        },
        "print": {
          "body": "site-print DTTT_Print"
        },
      });
    }
  },
  init: function(context) {
    if (!$.fn.dataTable) return;

    var defaults = $.components.getDefaults("dataTable");

    $('[data-plugin="dataTable"]', context).each(function() {
      var options = $.extend(true, {}, defaults, $(this).data());

      $(this).dataTable(options);
    });
  }
});

根据工具的ID我想打印uses_ids值,即如果我搜索228我应该得到92440 16824。 我曾尝试过 -

<?xml version="1.0" encoding="UTF-8"?>
<Tool_Parent>
    <tool name="ABCD" id="226">
        <category>Centralized</category>
        <extension_id>0</extension_id>
        <uses_ids>16824943 16824944</uses_ids>
    </tool>
    <tool name="EFGH" id="228">
        <category>Automated</category>
        <extension_id>0</extension_id>
        <uses_ids>92440 16824</uses_ids>
    </tool>
</Tool_Parent>

其中Tool_poco是包含成员变量声明的工具节点的poco类。 现在我想在toolData变量中获取与特定工具id相关的信息。如何做?

注意:我的变量如 -

var toolData = (from toolElement in doc.Descendants("tool")
               select new Tool_poco
               {
                a_Name = tool.Attribute("name").Value,
                a_Id = tool.Attribute("id").Value,
                e_ExtensionId = tool.Element("extension_id").Value,
                e_UsesIds =tool.Element("uses_parm_ids").Value
               });

请让我知道一种方法,我可以通过它修改toolData的上述查询。**

2 个答案:

答案 0 :(得分:0)

一旦你加载并准备好XDocument对象,你可以从做这样的事情开始:

var xdoc = XDocument.Parse(
            @"<?xml version=""1.0"" encoding=""utf-8""?>
                <Tool_Parent>
                  <tool name=""ABCD"" id=""226"">
                    <category>Centralized</category>
                    <extension_id>0</extension_id>
                    <uses_ids>16824943 16824944</uses_ids>
                  </tool>
                  <tool name=""EFGH"" id=""228"">
                    <category>Automated</category>
                    <extension_id>0</extension_id>
                    <uses_ids>92440 16824</uses_ids>
                  </tool>
                </Tool_Parent>");


        var root = xdoc.Root; // Got to have that root

        if (root != null)
        {
            var id228query = (from toolElement in root.Elements("tool")
                where toolElement.HasAttributes
                where toolElement.Attribute("id").Value.Equals("228")
                let xElement = toolElement.Element("uses_ids")
                where xElement != null
                select xElement.Value).FirstOrDefault();

            Console.WriteLine(id228query);

            Console.Read();
        }


Output: 92440 16824


**Note: In reference to your example, one possible reason it was not working
for you could be that your xml references an element with name "uses_ids",
however, your query references an element with a similar, but not exact, 
spelling with name "uses_parm_ids".** 

答案 1 :(得分:0)

您可以将查询修改为

 Tool_poco toolData = (from el in xelement.Elements("Employee")
                        where (string)el.Attribute("id") == "226"
                        select new Tool_poco
                        {
                            a_Name = el.Attribute("name").Value,
                            a_Id = el.Attribute("id").Value,
                            e_ExtensionId = el.Element("Name").Value,
                            e_UsesIds = el.Element("uses_ids").Value
                        }).FirstOrDefault();