C# 不确定如何在C中使用XmlNode

C# 不确定如何在C中使用XmlNode,c#,xml,xmldocument,xmlnode,C#,Xml,Xmldocument,Xmlnode,在C中,我需要使用XmlNode从这些属性中获取值,如下所示: 根元素ServerConfig: 类型 版本 创建日期 子节点项: 名字 来源 目的地 XML: 非常感谢您的示例代码:您应该使用XPath获取项目并循环结果 大概是这样的: foreach (XmlNode item in doc.DocumentElement.SelectNodes("items/item")) { var Name = item.Attributes["name"].Value; var Source

在C中,我需要使用XmlNode从这些属性中获取值,如下所示:

根元素ServerConfig:

类型

版本

创建日期

子节点项:

名字

来源

目的地

XML:


非常感谢您的示例代码:

您应该使用XPath获取项目并循环结果

大概是这样的:

foreach (XmlNode item in doc.DocumentElement.SelectNodes("items/item"))
{
  var Name = item.Attributes["name"].Value;
  var Source= item.Attributes["source"].Value;
  var Destination = item.Attributes["destination"].Value;
} 

要获取根元素,可以使用doc.DocumentElement

加载您的文档:

XmlDocument doc = new XmlDocument();
doc.Load(fileManager.ConfigFile);
然后,您将能够使用XPath选择任何内容:

doc.SelectSingleNode(XPath); 
doc.SelectNodes(XPath);
在最新的.Net版本中,您可以使用哪个更好

var xdoc = XDocument.Load(fileManager.ConfigFile);
var serverConfig = xdoc.Root;
string version = (string)serverConfig.Attribute("version");
DateTime date = (DateTime)serverConfig.Attribute("createDate");
string type = (string)serverConfig.Attribute("type");

var items = from item in serverConfig.Element("items").Elements()
            select new {
                Name = (string)item.Attribute("name"),
                Type = (string)item.Attribute("type"),
                Source = (string)item.Attribute("source"),
                Destination = (string)item.Attribute("destination")
            };
看一看——几行代码和文件被解析为强类型变量。偶数日期是DateTime对象,而不是字符串。和项是匿名对象的集合,其属性与xml属性相对应。

您可以使用:

课程:

public class ServerConfig
{
    public ServerConfig()
    {
        Items = new List<Item>();
    }

    [XmlAttribute("type")]
    public string Type { get; set; }

    [XmlAttribute("version")]
    public string Version { get; set; }

    [XmlAttribute("createDate")]
    public DateTime CreateDate { get; set; }

    [XmlArray("items")]
    [XmlArrayItem("item")]
    public List<Item> Items { get; set; }
}

public class Item
{
    [XmlAttribute("name")]
    public string Name { get; set; }

    [XmlAttribute("type")]
    public string Type { get; set; }

    [XmlAttribute("source")]
    public string Source { get; set; }

    [XmlAttribute("destination")]
    public string Destination { get; set; }

    [XmlAttribute("action")]
    public string Action { get; set; }
}
例如:

var data = @"<?xml version=""1.0"" encoding=""utf-8""?>
    <ServerConfig type=""ProjectName"" version =""1.1.1.2"" createDate =""2013-07-30T15:07:19.3859287+02:00"" >
    <items>
        <item  name=""fs"" type=""directory"" source=""C:\temp\source"" destination=""C:\temp\target"" action=""Create"" />
        <item  name=""testdoc.txt"" type=""file"" source=""C:\temp\source"" destination=""C:\temp\target"" action=""Update"" />
    </items>
    </ServerConfig>";

var serializer = new XmlSerializer(typeof(ServerConfig));

ServerConfig config;

using(var stream = new StringReader(data))
using(var reader = XmlReader.Create(stream))
{
    config = (ServerConfig)serializer.Deserialize(reader);
}
我不使用XmlNode,而是使用XmlElement,因为它有一个名为GetAttributestring attributeName的好方法,与XmlNode上的Attributes indexer属性相比,该方法更易于使用。而且,由于XmlElement派生自XmlNode,因此在保留基本XmlNode类的功能的同时,您可以获得附加的功能

public class ServerConfig
{
    public ServerConfig()
    {
        Items = new List<Item>();
    }

    [XmlAttribute("type")]
    public string Type { get; set; }

    [XmlAttribute("version")]
    public string Version { get; set; }

    [XmlAttribute("createDate")]
    public DateTime CreateDate { get; set; }

    [XmlArray("items")]
    [XmlArrayItem("item")]
    public List<Item> Items { get; set; }
}

public class Item
{
    [XmlAttribute("name")]
    public string Name { get; set; }

    [XmlAttribute("type")]
    public string Type { get; set; }

    [XmlAttribute("source")]
    public string Source { get; set; }

    [XmlAttribute("destination")]
    public string Destination { get; set; }

    [XmlAttribute("action")]
    public string Action { get; set; }
}
var data = @"<?xml version=""1.0"" encoding=""utf-8""?>
    <ServerConfig type=""ProjectName"" version =""1.1.1.2"" createDate =""2013-07-30T15:07:19.3859287+02:00"" >
    <items>
        <item  name=""fs"" type=""directory"" source=""C:\temp\source"" destination=""C:\temp\target"" action=""Create"" />
        <item  name=""testdoc.txt"" type=""file"" source=""C:\temp\source"" destination=""C:\temp\target"" action=""Update"" />
    </items>
    </ServerConfig>";

var serializer = new XmlSerializer(typeof(ServerConfig));

ServerConfig config;

using(var stream = new StringReader(data))
using(var reader = XmlReader.Create(stream))
{
    config = (ServerConfig)serializer.Deserialize(reader);
}
var items = doc.DocumentElement.SelectNodes("items/item").Cast<XmlElement>().ToList();

// You can iterate over the list, here's how you'd get your attributes:
var Name = items[0].GetAttribute("name"); // Returns null if attribute doesn't exist, doesn't throw exception
var Source = items[0].GetAttribute("source");
var Destination = items[0].GetAttrubite("destination");