2015-12-31 102 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; } 
    } 
} 
​