开发者

How would I use LINQ to XML to get the value from example XML

Here is the xml I am trying to access:

<resourceStrings>    
    <globalStrings>
          <string>
             <key>RptTitle1</key>
             <value>Title1</value>
           </string>    
           <string>
              <key>RptTitle2</key>
              <value>ReportTitle2</value>
           </string>
            <string>
                <key>RptTitle3</key>
                <value>ReportTitle3</value>
            </string开发者_JAVA百科>
       </globalStrings>
</resourceStrings>

How would I use linq to xml to search for key of RptTitle1 and return the value of the value node?


Like this:

var doc = XDocument.Load(...);

var setting = doc.Descendants("string").First(e => e.Element("key").Value == "RptTitle1");
var RptTitle1 = setting.Element("value").Value;

This code will find the first <string> element that has a <key> element with a value equal to RptTitle1, then get that element's <value> element.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜