2015-12-31 77 views
0

我有描述连接的XML文件重复的结构,即读取XML文件使用C#

<?xml version="1.0" encoding="utf-8"?> 
    <connections> 
     <connection name="Local server remote"> 
     <ip>192.168.0.7  </ip> 
     <port>23  </port> 
     <description> 
      Remote controlling of that nice & neat box under the table. 
     </description> 
     </connection> 
     <connection name="Far, far away server HTTP access"> 
      <ip>77.32.57.144  </ip> 
      <port>8080  </port> 
      <description> 
      A legend tells of a far, far away country of Russia, and of a server somewhere inside this mysterious country. 
      </description> 
     </connection> 
     </connections> 

有一种简单的方法来解析XML文件转换成像一个类的对象:

class Connection { 
     public string Name; 
     public string IP; 
     public int Port; 
     public string Description; 
    } 

+0

我建议你看看这个StackOverflow的答案:http://stackoverflow.com/questions/608110/is-it-possible-to-deserialize-xml-into-listt因为它正在做你正在做的事情。 – Adrian

+0

@阿德里安,谢谢 –

回答

1

你可以使用LINQ到XML在阅读本:

var xmlDoc = XDocument.Load("XMLFile1.xml"); 
if(xmlDoc.Root != null) 
{ 
    var connections = (xmlDoc.Root.Elements("connection").Select(e => new Connection 
    { 
     Description = (string) e.Element("description"), 
     IP = (string) e.Element("ip"), 
     Name = (string) e.Element("name"), 
     Port = (int) e.Element("port") 
    })).ToList(); 
} 

也许值得指出你的XML文件包含一个&字符,它将抛出XML文档加载。我能够运行上述代码通过替换&符号&amp;

1

您将不得不创建一个包装类型来包含连接列表,因为它不知道<connections>是什么,然后通过为每个字段添加一个XmlElement名称来照顾其余部分。

public static void Main(string[] args) 
{ 
    var serializer = new XmlSerializer(typeof (ConnectionList)); 

    var connections = ((ConnectionList)serializer.Deserialize(new StreamReader("data.xml"))).Connections; 
    foreach (var connection in connections) 
    { 
     Console.WriteLine(connection.IP); 
    } 
    Console.ReadLine(); 
} 

[XmlRoot("connections")] 
public class ConnectionList 
{ 
    [XmlElement("connection")] 
    public List<Connection> Connections { get; set; } = new List<Connection>(); 
} 

[XmlType("connection")] 
public class Connection 
{ 
    [XmlElement("description")] public string Description; 

    [XmlElement("ip")] public string IP; 

    [XmlElement("name")] public string Name; 

    [XmlElement("port")] public int Port; 
} 

注: '&' 的XML是无效字符,并且必须转义为&amp;

0

试试这个。最好的方法

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      XDocument doc = XDocument.Load(FILENAME); 
      List<Connection> connections = doc.Descendants("connection").Select(x => new Connection(){ 
       Name = x.Attribute("name").Value, 
       IP = x.Element("ip").Value, 
       Port = int.Parse(x.Element("port").Value), 
       Description = x.Element("description").Value, 
      }).ToList(); 
     } 
    } 
    public class Connection 
    { 
     public string Name { get; set; } 
     public string IP { get; set; } 
     public int Port { get; set; } 
     public string Description { get; set; } 
    } 
} 
​