【问题标题】:Select XML elements based on value根据值选择 XML 元素
【发布时间】:2015-08-08 14:52:22
【问题描述】:

我有简单的 XML。我想在 VB.NET WindowsForm 应用程序中选择 CASE where Section = "A" by LINQ。我将不胜感激任何帮助。提示可以在 C# 中。谢谢。

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <PredefinedText>
        <Section>A</Section>
        <CASE>Case 1A</CASE>
    </PredefinedText>
    <PredefinedText>
        <Section>B</Section>
        <CASE>Case 1B</CASE>
    </PredefinedText>
</data>

我有这段代码,但它不会向 ComboBox 返回任何记录...

sSettingsFilePath = "Sections.xml"

Dim xelement As XElement = xelement.Load(sSettingsFilePath)

ComboBox1.DataSource = (From cases In xelement.Elements("PredefinedText") _
                            Select cases.Element("CASE").Value _
                            Where xelement.Element("PredefinedText").Element("Section").Value = "A"
                            Order By Value).ToList()

【问题讨论】:

  • 你有什么问题?
  • 你的 xml 查询返回你想要的。
  • 嗨,Maciej 和琼斯。谢谢您的答复。查询结果是我得到了所有部分的所有案例,不限于“A”部分。我只想得到“案例 1A”。

标签: c# xml vb.net linq select


【解决方案1】:

用于选择 CASE 元素,

ComboBox1.DataSource = xelement
                           .Descendants("PredefinedText")
                           .Where(i => i.Element("Section").Value == "A")
                           .Select(j => j.Element("CASE").Value);

【讨论】:

  • 嗨@Siva Charan。如果您添加 .ToList() 函数,这将非常有用。在 VB.NET 中是 ComboBox1.DataSource = xelement.Descendants("PredefinedText").Where(Function(i) i.Element("Section").Value = "A").[Select](Function(j) j. Element("CASE").Value).ToList()
【解决方案2】:

试试……

sSettingsFilePath = "Sections.xml"
Dim doc As XDocument = XDocument.Load(new FileStream(sSettingsFilePath, FileMode.Open))
ComboBox1.DataSource = (From x In doc.root.Elements("PredefinedText")
                        Where x.Element("Section").Value = "A"
                        Select x.Element("CASE").Value).ToList()

【讨论】:

    【解决方案3】:

    问题是xelement 已经是PredefinedText,所以xelement.Element("PredefinedText").Element("Section").Value 永远不会返回现有对象(PredefinedText 中没有子PredefinedText)。 因此,只需将您的语句转换为:

    ComboBox1.DataSource = (From cases In xelement.Elements("PredefinedText") _
                                Select cases.Element("CASE").Value _
                                Where xelement.Element("Section").Value = "A"
                                Order By Value).ToList()
    

    看起来更用户友好的第二个选项是这样编写代码:

    xelement.Elements("PredefinedText")
                    .Where(x => x.Element("Section").Value == "A")
                    .OrderBy(x=>x.Value);
    

    【讨论】:

    • 嗨,加拉斯。感谢您的解决方案。对于我,这说得通。我已经尝试过您发布的第一个代码。我在“Where”语句的行上得到“Null Reference Exception”。
    【解决方案4】:

    试试这个:

    XDocument xDoc = XDocument.Load("fullfilepath.xml");
    var qry = xDoc.Root.Descendants("Section")
        .Where(ele=>ele.Element("PredefinedText").Value=="A") //or StartsWith("A") or Contains("A")
        .OrderBy(ele=>ele.Value)
        .ToList();
    

    如需更多信息,请参阅:
    XDocument.Load Method
    Samples (LINQ to XML)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      相关资源
      最近更新 更多