我对Linq XML查询感到困惑

时间:2012-11-28 16:08:20

标签: c# xml linq

这是我的XML示例。我想在ID = 123选择SystemSetting的值。但我无法弄清楚如何。如果id的值等于123,我如何选择SystemSetting值?

<?xml version="1.0" encoding="utf-8" ?>
<Private>
  <System>
    <ID>123</ID>
    <NAME>Test</NAME>
    <SystemSetting>128</SystemSetting>
    <SystemSettingCS>127</SystemSettingCS>
  </System> 
  <System>
    <ID>124</ID>
    <NAME>Test2</NAME>
    <SystemSetting>128</SystemSetting>
    <SystemSettingCS>127</SystemSettingCS>
  </System>
  <System>
    <ID>0</ID>
    <NAME>Test</NAME>   
    <SystemSetting>5</SystemSetting>
    <SystemSettingCS>250</SystemSettingCS>
  </System>
</Private>

这是我试过的:

    var doc = XDocument.Load(Application.StartupPath+  @"\Settings.xml");
    var q = from Ana in doc.Descendants("Private")                   
            from sistem in Ana.Elements("System")                   
            where (int)sistem.Element("ID") == 123
            from assetText in Sistem.Elements("System")
            select assetText.Element("SystemSetting");

    MessageBox.Show(q.ToString());

thnx寻求帮助。

5 个答案:

答案 0 :(得分:2)

我认为你使这比你需要的更复杂。我想你只需要:

var query = doc.Descendants("Private") // Or just doc.Root
               .Elements("System")
               .Where(x => (int) x.Element("ID") == 123)
               .Select(x => x.Element("SystemSetting"))
               .FirstOrDefault();

无可否认,这将选择第一个匹配元素。 query的类型为XElement;如果您取消FirstOrDefault()部分,则会为所有匹配元素返回IEnumerable<XElement>

如果您只想要值而不是元素,则可以将Select更改为:

.Select(x => (string) x.Element("SystemSetting"))

.Select(x => x.Element("SystemSetting").Value)

(如果没有null元素,第一个将返回SystemSetting;第二个将返回异常。)

答案 1 :(得分:1)

Xpath( System.Xml.XPath )在这里可以提供帮助

var system = doc.XPathSelectElement("//System[ID[text()='123']]");
var val = system.Element("SystemSetting").Value;

或单行

var s = (string)doc.XPathSelectElement("//System[ID[text()='123']]/SystemSetting");

答案 2 :(得分:0)

 var q = from s in doc.Descendants("System")  
         where (int)s.Element("ID") == 123        
         select (int)s.Element("SystemSetting");

并显示结果(q将有IEnumerable<int>类型):

if (q.Any())
    MessageBox.Show("SystemSettings = " + q.First());
else
    MessageBox.Show("System not found");

答案 3 :(得分:0)

你几乎在那里

var xmlFile = XElement.Load(@"c:\\test.xml");

var query =
  from e in xmlFile.Elements()
  where e.Element("ID").Value == "123"
  select e.Element("SystemSetting").Value;

答案 4 :(得分:0)

是Linq的问题,但是有一种替代的XPath方法,但下面定义的类可以在任何一种情况下都有效。

定义要从父系统元素读取的类:

public class XSystem
{
   public XSystem(XElement xSystem) { self = xSystem; }
   XElement self;

   public int Id { get { return (int)self.Element("ID"); } }
   public string Name { get { return self.Element("NAME").Value; } }
   public int SystemSetting { get { return (int)self.Element("SystemSetting"); } }
   public int SystemSettingCS { get { return (int)self.Element("SystemSettingCS"); } }
}

然后找到系统元素,该元素的 ID 元素 123

int id = 123;
string xpath = string.Format("//System[ID={0}", id);
XElement x = doc.XPathSelectElement(xpath);

然后将其插入课程:

XSystem system = new XSystem(x);

然后读取您想要的值:

int systemSetting = system.SystemSetting; 

XPath定义为using System.Xml.XPath;

相关问题