简体   繁体   English

使用C#给定条件从XML获取值

[英]Getting values from XML with conditions given using C#

I'm kinda new with using XML with C#. 我对在C#中使用XML有点陌生。

XML CODE: XML代码:

<LVL2>
  <Tables>
    <TBL_ID>1</TBL_ID>
    <TBL_Name>test1</TBL_Name>
    <MD_ID>1</MD_ID>
  <Tables>
  <Tables>
    <TBL_ID>2</TBL_ID>
    <TBL_Name>test2</TBL_Name>
    <MD_ID>1</MD_ID>
  </Tables>
  <Tables>
    <TBL_ID>3</TBL_ID>
    <TBL_Name>test3</TBL_Name>
    <MD_ID>1</MD_ID>
  </Tables>
</LVL2>

<LVL2>
  <Tables>
    <TBL_ID>1</TBL_ID>
    <TBL_Name>test4</TBL_Name>
    <MD_ID>2</MD_ID>
  </Tables>
  <Tables>
    <TBL_ID>2</TBL_ID>
    <TBL_Name>test5</TBL_Name>
    <MD_ID>2</MD_ID>
  </Tables>
  <Tables>
    <TBL_ID>3</TBL_ID>
    <TBL_Name>test6</TBL_Name>
    <MD_ID>2</MD_ID>
  </Tables>
</LVL2>

How do I insert to a checkedlistbox the text values from tbl_name that has an md_id = 1 only. 如何将tbl_name中只有md_id = 1的文本值插入到复选框md_id = 1 Here is my current code. 这是我当前的代码。

while (xmlReader.Read())
{
    switch (xmlReader.NodeType)
    {

        case XmlNodeType.Element:
            elName = xmlReader.Name;
            break;
        case XmlNodeType.Text:

            if (elName == "TBL_Name" && MD_ID == "1")
            {
                checkedListBox2.Items.Add(xmlReader.Value);
            }

            break;     
    }
}

I can't seem to figure out on how to get the text that has MD_ID = "1" and output: 我似乎无法弄清楚如何获取具有MD_ID = "1"的文本并输出:

test4
test5
test6

First of all, the xml is not formatted properly. 首先,xml格式不正确。 It should contain a root node and you missed the closing of a <Tables> tag. 它应该包含一个根节点,并且您错过了<Tables>标记的关闭。 In the sample example, if you want to choose table names of elements having "MD_ID = 1" , the result will be: 在示例示例中,如果要选择具有“ MD_ID = 1”的元素的表名,结果将是:

test1 测试1
test2 测试2
test3 测试3

If you want o/p as you mentioned, then the condition will be not equal to 1. Here is the solution: 如果您希望如前所述进行o / p,则条件将不等于1。这是解决方案:

string xmlInput =  @"
    <root>
    <LVL2>
    <Tables>
     <TBL_ID>1</TBL_ID>
     <TBL_Name>test1</TBL_Name>
     <MD_ID>1</MD_ID>
   </Tables>
   <Tables>
    <TBL_ID>2</TBL_ID>
    <TBL_Name>test2</TBL_Name>
    <MD_ID>1</MD_ID>
   </Tables>
   <Tables>
    <TBL_ID>3</TBL_ID>
    <TBL_Name>test3</TBL_Name>
    <MD_ID>1</MD_ID>
   </Tables>
   </LVL2>
   <LVL2>
   <Tables>
    <TBL_ID>1</TBL_ID>
    <TBL_Name>test4</TBL_Name>
    <MD_ID>2</MD_ID>
   </Tables>
   <Tables>
    <TBL_ID>2</TBL_ID>
    <TBL_Name>test5</TBL_Name>
    <MD_ID>2</MD_ID>
   </Tables>
   <Tables>
    <TBL_ID>3</TBL_ID>
    <TBL_Name>test6</TBL_Name>
    <MD_ID>2</MD_ID>
   </Tables>
  </LVL2>
  </root>";

XDocument  xdoc = XDocument.Parse(xmlInput);

var filteredXML =
  xdoc.Descendants("root")
    .Elements("LVL2")
    .Elements("Tables")
    .Where(x => string.Compare(x.Element("MD_ID").Value, "1") == 0)
    .Select(x => x.Element("TBL_Name").Value)
    .ToList();

Console.WriteLine(filteredXML);

Refer following namespace: 请参考以下名称空间:

using System.Xml.Linq;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM