4

これが私のXMLサンプルです。の場合、SystemSettingの値を選択したいID = 123。しかし、私はその方法を理解できません。SystemSettingidの値が123に等しい場合、どうすれば値を選択できますか?

<?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。

4

5 に答える 5

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();

確かに、これにより最初に一致する要素が選択されます。その場合のタイプqueryXElement; パーツを外すと、一致するすべての要素に対して、FirstOrDefault()が返されます。IEnumerable<XElement>

要素の代わりに値だけが必要な場合は、次のように変更できますSelect

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

また

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

null(最初の要素は要素がない場合に返さSystemSettingれ、2番目の要素は例外をスローします。)

于 2012-11-28T16:11:30.420 に答える
1

Xpath(System.Xml.XPath)はここで本当に役立ちます

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

または1行で

var s = (string)doc.XPathSelectElement("//System[ID[text()='123']]/SystemSetting");
于 2012-11-28T16:13:25.413 に答える
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");
于 2012-11-28T16:13:01.547 に答える
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;
于 2012-11-28T16:17:03.720 に答える
0

Linqの質問でしたが、別のXPathアプローチがありますが、以下に定義されているクラスはどちらのシナリオでも機能します。

親System要素から読み取るクラスを定義します。

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"); } }
}

次に、 123の子ID要素を持つSystem要素を見つけます。

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;

于 2012-11-28T18:01:52.163 に答える