1

我正在尝试阅读此 XML 文件:

<server>
  <Version date="06/05/2013">2.5.4</Version>
  <Lastfix path="\">Now reading basic config files! Read the README file!</Lastfix>
  <Version date="07/05/2013">2.5.3</Version>
  <Lastfix path="\wServer\ClientProcessor.cs">Fixed character creation bugs (related to mysql, download and import the new struct.sql file under db!)</Lastfix>
  <Version date="06/05/2013">2.5.0</Version>
  <Lastfix path="\">BIG stability fix, register fix, buy gold fix</Lastfix>
  <Version date="02/05/2013">2.4.1</Version>
  <Lastfix path="\wServer\realm\entities\player\Player.UseItem.cs">Almost finished dungeon keys!</Lastfix>
  <Version date="30/04/2013">2.3.1</Version>
  <Lastfix path="\wServer\realm\entities\player\Player.UseItem.cs">Dungeon keys now working!</Lastfix>
  <Version date="30/04/2013">2.3.0</Version>
  <Lastfix path="\wServer\realm\entities\player\Player.Chat.cs">Added /who, /tell, /server commands!</Lastfix>
</server>

但无法弄清楚如何获取版本和 lastfix 文本值!

我正在尝试这段代码。

XmlDocument version = new XmlDocument();
version.Load(path);

foreach (XmlNode node in version.SelectNodes("server"))
{
    MessageBox.Show(node.SelectSingleNode("lastfix_date").InnerText);
}

但没有成功

4

3 回答 3

1

可能不像 xmlpath 解决方案那样“正确”,但我喜欢尽可能使用 linq Aggregate(),所以我提出了我简陋的解决方案:

void Main()
{
  XmlDocument v = new XmlDocument();
  v.LoadXml(@"<server>
<Version date=""06/05/2013"">2.5.4</Version>
<Lastfix path=""\"">Now reading basic config files! Read the README file!</Lastfix>
<Version date=""07/05/2013"">2.5.3</Version>
<Lastfix path=""\wServer\ClientProcessor.cs"">Fixed character creation bugs (related to mysql, download and import the new struct.sql file under db!)</Lastfix>
<Version date=""06/05/2013"">2.5.0</Version>
<Lastfix path=""\"">BIG stability fix, register fix, buy gold fix</Lastfix>
<Version date=""02/05/2013"">2.4.1</Version>
<Lastfix path=""\wServer\realm\entities\player\Player.UseItem.cs"">Almost finished dungeon keys!</Lastfix>
<Version date=""30/04/2013"">2.3.1</Version>
<Lastfix path=""\wServer\realm\entities\player\Player.UseItem.cs"">Dungeon keys now working!</Lastfix>
<Version date=""30/04/2013"">2.3.0</Version>
<Lastfix path=""\wServer\realm\entities\player\Player.Chat.cs"">Added /who, /tell, /server commands!</Lastfix>
</server>");

  var result = v.SelectSingleNode("server").ChildNodes.OfType<XmlNode>().Aggregate(
     new List<item>(),
     (list, node) => {
      if (node.Name == "Version")
      {
        list.Add(new item { versionD = node.Attributes["date"].Value, versionN = node.InnerText, lastfix = "" });
        return list;
      }
      else
      {
        list.Last().lastfix = node.InnerText;
        return list;
      }
    });

  result.Dump();
}

public class item {
  public string versionD { get; set; }
  public string versionN { get; set; }
  public string lastfix { get; set; }
}

返回

结果

在 linqPad 中运行时(在 linqPad.com)

于 2013-06-22T19:06:57.517 回答
0

尝试这个

XmlDocument version = new XmlDocument();
version.Load(path);

foreach (XmlNode node in version.ChildNodes[0].ChildNodes)
{
    if (node.Name == "Version")
        MessageBox.Show("Version: " + node.InnerText);
    else if (node.Name == "Lastfix")
        MessageBox.Show("LastFix: " + node.InnerText);
}
于 2013-06-22T19:34:00.063 回答
0

这是一个基于 Linq 的解决方案(您正在使用 XmlDocument,但不要指定您必须)

XDocument xDoc = XDocument.Load(@"C:\myServerFile.xml");
List<string> serverVersions = new List<string>();
foreach (var element in xDoc.Elements("server").Elements("Version"))
{
       string version = (string)element + " " + (string)((XElement)element.NextNode);
       serverVersions.Add(version);
}

然后,您可以使用“serverVersions”字符串列表做任何您想做的事情

于 2013-06-22T19:09:03.113 回答