How to read XML file using XmlSerializer C# [duplicate]

安稳与你 提交于 2019-12-04 02:35:19

问题


I have a problem when I want to read XML file by using XmlSerializer. My xml file like follow :

<?xml version="1.0" encoding="utf-8"?>
<contents>
  <section id="1">   
    <element1>2</element1>
    <element2>1</element2>
    <idx>1</idx>  
    <idx>2</idx>  
    <idx>3</idx>    
  </section>

  <section id="2">
    <element1>2</element1>
    <element2>1</element2>    
  </section>

  <section id="3"/>
</contents>

Here are the classes:

 [Serializable()]
 public class section
 {
     [XmlAttribute("id")]
     public string id { get; set; }

     [XmlElement("element1")]
     public int element1 { get; set; }

     [XmlElement("element2")]
     public int element2 { get; set; }


     [XmlElement("idx")]
     public int[] idx { get; set; }


 }
     [Serializable()]
     [XmlRoot("contents")]
     public class contents
     {
         [XmlArray("section")]
         [XmlArrayItem("section", typeof(section))]
         public section[] section { get; set; }
     }

The Deserialize function:

             XmlSerializer serializer = new XmlSerializer(typeof(contents));


             FileStream fs = new FileStream(path, FileMode.Open);
             XmlReader reader = XmlReader.Create(fs);


             contents i;


             i = (contents)serializer.Deserialize(reader);
             fs.Close();




             foreach (section p in i.section)
             {
                 Console.WriteLine(p.element1);
             }

Why it doesn't work? I had reference https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx but it seem not useful. Please help me!!!!!


回答1:


Try this...

Usings...

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

Classes...(created from your XML using http://xmltocsharp.azurewebsites.net/)

    [XmlRoot(ElementName = "section")]
    public class Section
    {
        [XmlElement(ElementName = "element1")]
        public string Element1 { get; set; }
        [XmlElement(ElementName = "element2")]
        public string Element2 { get; set; }
        [XmlElement(ElementName = "idx")]
        public List<string> Idx { get; set; }
        [XmlAttribute(AttributeName = "id")]
        public string Id { get; set; }
    }

    [XmlRoot(ElementName = "contents")]
    public class Contents
    {
        [XmlElement(ElementName = "section")]
        public List<Section> Section { get; set; }
    }

Code...

        Contents dezerializedXML = new Contents();
        // Deserialize to object
        XmlSerializer serializer = new XmlSerializer(typeof(Contents));
        using (FileStream stream = File.OpenRead(@"xml.xml"))
        {
            dezerializedXML = (Contents)serializer.Deserialize(stream);
        } // Put a break-point here, then mouse-over dezerializedXML

I put your XML in a file (xml.xml) and read it from there....



来源:https://stackoverflow.com/questions/36202703/how-to-read-xml-file-using-xmlserializer-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!