簡體   English   中英

在C#中,我將如何使用xml.linq來獲取嵌套在xml中的值?

[英]In C# how would I use xml.linq to grab values nested in xml?

抱歉,如果有人問我找不到它。 我對XML不太滿意,但正在嘗試使用XML

xml看起來像這樣:

<?xml version="1.0" encoding="utf-8" ?>
<Config>
  <commands>
    <command name="check">
      <stdout>output_value</stdout>
    </command>
    <command name="ping http://192.168.1.1">
      <stdout>result</stdout>
    </command>
    <command name="config">
      <stdout>Initialized</stdout>
      <stdout>listen on http://127.0.0.1:8888</stdout>
      <stdout>loaded</stdout>
      <stdout>verified</stdout>
      <stdout>connected</stdout>
      <stdout>connection INITIALIZED</stdout>
      <stdout>load complete</stdout>
    </command>
    <command name="connection">
      <stdout>Got connection</stdout>
    </command>
  </commands>
</Config>

基本上我試圖在每個命令下獲取文本在stdout中的位置。

我已經知道我可以通過以下方式獲得“命令名稱”的顯示位置

XDocument xd = XDocument.Load("config.xml");
var commandcheck = from paths in xd.Decendants(xmln)
                   select paths.Attribute(attrb).value
foreach(var paths in commandcheck)
{
   Console.WriteLine(paths)
}

基本上,我嘗試解析和顯示此數據的方式為

Command name Check
stdout output_value

command name config
stdout initialized
stdout listen
stdout loaded 

等等

謝謝。

您可以使用Element方法獲取所需的輸出。

var textcheck = from paths in xd.Descendants("command") select paths.Element("commandoutput").Value;
foreach (var paths in textcheck)
{
   Console.WriteLine(paths);//Here you get output as 'TEXT'
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM