不确定如何在C#中使用XmlNode(not sure how to use XmlNode in C#)

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

根元素(ServerConfig):

类型

CREATEDATE

子节点(Items):

名称

资源

目的地

XML:

<?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>

C#:

XmlTextReader reader = new XmlTextReader(fileManager.ConfigFile); XmlDocument doc = new XmlDocument(); XmlNode node = doc.ReadNode(reader); // failed to get values here var Version = node.Attributes["version"].Value; var Type = node.Attributes["type"].Value; var Date = node.Attributes["createDate"].Value; //how to get values from items/item attributes here?

您的示例代码非常感谢谢谢:)

In C# I need to use XmlNode to get values from these attributes as follows:

Root element (ServerConfig):

type

version

createDate

Child nodes (Items):

name

source

destination

XML:

<?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>

C#:

XmlTextReader reader = new XmlTextReader(fileManager.ConfigFile); XmlDocument doc = new XmlDocument(); XmlNode node = doc.ReadNode(reader); // failed to get values here var Version = node.Attributes["version"].Value; var Type = node.Attributes["type"].Value; var Date = node.Attributes["createDate"].Value; //how to get values from items/item attributes here?

Your example code much appreciated thanks :)

最满意答案

您可以使用LINQ to XML (在最新的.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") };

看一看 - 几行代码和文件解析为强类型变量。 甚至date是DateTime对象而不是string。 并且是具有与xml属性对应的属性的匿名对象的集合。

You can use LINQ to XML (which is preferable in latest .Net versions)

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") };

Take a look - few lines of code and file parsed into strongly-typed variables. Even date is a DateTime object instead of string. And items are collection of anonymous objects with properties corresponding to xml attributes.

更多推荐