2011-12-22 14 views

回答

3

我爲您準備了一份工作樣本,您可以繼續探索。

using System; 
using System.Linq; 
using System.Xml; 
using System.Xml.Linq; 
using System.Xml.Serialization; 
using System.Collections.Generic; 
using System.IO; 


namespace ConsoleApplication5 
{ 
    public class Person 
    { 
    public int Age { get; set; } 
    public string Name { get; set; } 
    public int XMLLine { get; set; } 
    } 
    public class Persons : List<Person> { } 

    class Program 
    { 

    static void Main(string[] args) 
    { 
     //create your objects 

     Person p = new Person(); 
     p.Age = 35; 
     p.Name = "Arnold"; 

     Person p2 = new Person(); 
     p2.Age = 36; 
     p2.Name = "Tom"; 

     Persons ps = new Persons(); 
     ps.Add(p); 
     ps.Add(p2); 

     //Serialize them to XML 

     XmlSerializer xs = new XmlSerializer(typeof(Persons)); 

     XDocument d = new XDocument(); 

     using (XmlWriter xw = d.CreateWriter()) 
     xs.Serialize(xw, ps); 

     //print xml 
     //System.Diagnostics.Debug.WriteLine(d.ToString()); 

     // it will produce following xml. You can save it to file. 
     //I have saved it to variable xml for demo 



     string xml = @"<ArrayOfPerson> 
         <Person> 
         <Age>35</Age> 
         <Name>Arnold</Name> 
         <XMLLine>0</XMLLine> 
        </Person> 
        <Person> 
         <Age>36</Age> 
         <Name>Tom</Name> 
         <XMLLine>0</XMLLine> 
         </Person> 
        </ArrayOfPerson>"; 




     XDocument xdoc = XDocument.Parse(xml, LoadOptions.SetLineInfo); 

     // A little trick to get xml line 
     xdoc.Descendants("Person").All(a => { a.SetElementValue("XMLLine", ((IXmlLineInfo)a).HasLineInfo() ? ((IXmlLineInfo)a).LineNumber : -1); return true; }); 


     //deserialize back to object 

     Persons pplz = xs.Deserialize((xdoc.CreateReader())) as Persons; 

     pplz.All(a => { Console.WriteLine(string.Format("Name {0} ,Age{1} ,Line number of object in XML File {2}", a.Name, a.Age, a.XMLLine)); return true; }); 

     Console.ReadLine(); 

    } 
    } 
} 

,它會給你的結果一樣

名稱阿諾德,Age35,在XML文件對象的行號2

名稱湯姆,Age36,在XML文件7對象的行號


+1

謝謝。這正是我正在尋找的:)。 – AlexTheo 2011-12-23 11:27:14

+0

使用d.Root.RemoveAttributes();之後xs.Serialize(xw,ps);從根節點刪除所有屬性。 – Thulasiram 2014-10-28 10:48:17

0

你可以試試這個擴展方法:

public static string ToXml<T>(this object obj) 
{ 
    using (var memoryStream = new MemoryStream()) 
    { 
     using (TextWriter streamWriter = new StreamWriter(memoryStream)) 
     { 
      var xmlSerializer = new XmlSerializer(typeof(T)); 
      xmlSerializer.Serialize(streamWriter, obj); 
      return Encoding.ASCII.GetString(memoryStream.ToArray()); 
     } 
    } 
} 

public static void ToXmlFile<T>(this object obj, string fileName) 
{ 
    using (TextWriter streamWriter = new StreamWriter(fileName)) 
    { 
     var xmlSerializer = new XmlSerializer(typeof(T)); 
     xmlSerializer.Serialize(streamWriter, obj); 
    } 
} 

用法:

// you will get this on a string variable 
var xmlString = yourModel.ToXml<YourModel>(); 

// you will save our object in a file. 
yourModel.ToXmlFile<YourModel>(@"C:\yourModelDump.xml"); 

請注意在您的類添加SerializableAttribute

[Serializable] 
public class YourModel 
{ 
    //... 
} 

這應該這樣做

+0

它只會序列化我的對象,並會給我XML文本此解決方案有重複節點的問題。但我需要映射到文件。就像給串行器一些命令來存儲對象元數據中每個節點的行位置。 – AlexTheo 2011-12-22 12:02:10

+0

找不到你。你能詳細描述一下嗎? – 2011-12-22 12:03:15

+0

我的意思是,例如,如果我使用映射來讀取XML文檔,我想查找與當前對象相關的XML文檔的哪一行。在我想從某種列表中選擇我的對象並自動轉到XML文件以編輯它之後。 – AlexTheo 2011-12-22 12:07:58