使用C#将XML字符串解析并转换为对象列表?

时间:2015-03-16 11:55:18

标签: c# xml

我有以下XML字符串:

<rows>
  <row>
    <id>
      <old>2125</old>
    </id>
    <name>
      <old>test</old>
    </name>
    <amount>
      <old>62</old>
    </amount>
  </row>
</rows>

可以使用Linq将上述内容转换为以下类的列表:

public class Row
{
    public int Id;
    public string Fields;
}

其中Fields将包含以下格式的上述行:

id|2125^name|text^amount|62

3 个答案:

答案 0 :(得分:1)

我相信在你的情况下,你正在寻找XmlSerialization。以下是考试:

namespace consoleApp {  

    [XmlRoot()]  
    public class EventInput {  

        private string group;  

        public string Group {  
            get { return group; }  
            set { group = value; }  
        }  

        private Event[] events;  

        public Event[] Events {  
            get { return events; }  
            set { events = value; }  
        }          
    }  

    public class Event {  
        private int id;  

        [XmlAttribute]  
        public int Id {  
            get { return id; }  
            set { id = value; }  
        }  
    }  



    class Program {  

        public static void Main() {  

            string xml = @"  
                <EventInput> 
                    <Group>12345</Group> 
                    <Events> 
                        <Event Id=""100"" /> 
                        <Event Id=""101"" /> 
                        <Event Id=""102"" /> 
                        <Event Id=""103"" /> 
                        <Event Id=""104"" /> 
                        </Events> 
                </EventInput>";  

            XmlSerializer serializer = new XmlSerializer(typeof(EventInput));  
            EventInput ei = (EventInput)serializer.Deserialize(new StringReader(xml));  

            Console.WriteLine(ei.Group);  
            foreach(Event e in ei.Events) {  
                Console.WriteLine(e.Id);  
            }  

            Console.WriteLine("\n=============================\n");  

            ei = new EventInput() {  
                Group = "1111",  
                Events = new Event[] {   
                    new Event() { Id = 3},   
                    new Event() { Id = 7},   
                    new Event() { Id = 10}}  
            };  

            serializer.Serialize(Console.Out, ei);  
        }  
    }  
}

如果有更复杂的xml文档 - 如果您有模式 - 可以使用xsd.exe为您自动生成类层次结构。有关XmlSerialization属性的更多详细信息,请查看此处:http://msdn.microsoft.com/en-us/library/83y7df3e(VS.85).aspx

希望这有帮助

答案 1 :(得分:1)

使用linq到XML和一些foreach循环,你可以尝试如下。

    var str = @"<rows>
  <row>
    <id>
      <old>2125</old>
    </id>
    <name>
      <old>test</old>
    </name>
    <amount>
      <old>62</old>
    </amount>
  </row>
</rows>";

 var elements = XElement.Parse(str);

 var rows = elements.Elements("row");

  var list = new List<Row>();

  foreach(var row in rows)
  {
     var id = Int32.Parse(row.Element("id").Element("old").Value);

     var name = row.Element("name").Element("old").Value;

     var amount = row.Element("amount").Element("old").Value;

     var fields = string.Format("id|{0}^name|{1}^amount|{2}",id, name, amount);

     list.Add(new Row { Id = id, Fields = fields});
  }


}

答案 2 :(得分:0)

Linq本身不会这样做,但如果您使用XMLDocument或其他XML解释器将XML解析为对象集合,那么您可以使用linq来读取集合

https://msdn.microsoft.com/en-us/library/system.xml.xmldocument%28v=vs.110%29.aspx

所以

var rows = from d in xmlDoc.ChildNodes
           where d.Name = "row"
           select new {id= d.Attributes.FirstOrDefault(a=>a.Name=="id"),Other=d.Attributes.Where(a=>a.Name!="id");