读奇怪的xml doc

时间:2013-06-22 17:58:08

标签: c# xml

我正在尝试阅读此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);
}

但没有成功

3 个答案:

答案 0 :(得分: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; }
}

返回

result

在linqPad(linqPad.com)中运行时

答案 1 :(得分: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”字符串列表

执行任何操作

答案 2 :(得分: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);
}