Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Need your help in setting the xml attributes for XML deserialization.

This is my input xml:

<form>
<question id="QnA">
<answer>AnswerforA</answer>
</question>
<question id="QnB">
<answer>AnswerforB</answer>
</question>
<question id="QnC">
<answer>AnswerforC</answer>
</question>
</form>

The ids of each question element tag correspond to a class property and its value is the innertext of the corresponding answer element.

The .cs file will look like

 public class Test
{

   private string qnaAns;    
   private string qnbAns;   
   private string qncAns;   

    public string QnA
    {
    get{ return qnaAns;}
    set{qnaAns = value;}
    }

    public string QnB
    {
    get{ return qnbAns;}
    set{qnbAns = value;}
    }

    public string QnC
    {
    get{ return qncAns;}
    set{qncAns = value;}
    }
}

and I use the follwing code for deserialization

XmlSerializer ser = new XmlSerializer(typeof(Test));

XmlReader xr = new xmlReader(inputxml);

Test t = ser.Deserialize(xr) as Test;

Please let me know how to set the XML element/attribute for the Test class to achieve this.

Thanks for your time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
258 views
Welcome To Ask or Share your Answers For Others

1 Answer

 [XmlRoot("form")]
    public class Form
    {
        [XmlElement("question")]
        public List<Question> Questions { get; set; }

        public Form()
        {
            Questions = new List<Question>();
        }
    }
    public struct Question
    {
        [XmlAttribute("id")]
        public string ID { get; set; }

        [XmlElement("answer")]
        public string Answer { get; set; }
    }

Then to serialize, I use the following extensions:

public static bool XmlSerialize<T>(this T item, string fileName) 
        { 
            return item.XmlSerialize(fileName, true); 
        } 
        public static bool XmlSerialize<T>(this T item, string fileName, bool removeNamespaces) 
        { 
            object locker = new object(); 

            XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces(); 
            xmlns.Add(string.Empty, string.Empty); 

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); 

            XmlWriterSettings settings = new XmlWriterSettings(); 
            settings.Indent = true; 
            settings.OmitXmlDeclaration = true; 

            lock (locker) 
            { 
                using (XmlWriter writer = XmlWriter.Create(fileName, settings)) 
                { 
                    if (removeNamespaces) 
                    { 
                        xmlSerializer.Serialize(writer, item, xmlns); 
                    } 
                    else { xmlSerializer.Serialize(writer, item); } 

                    writer.Close(); 
                } 
            } 

            return true; 
        } 
        public static T XmlDeserialize<T>(this string s) 
        { 
            object locker = new object(); 
            StringReader stringReader = new StringReader(s); 
            XmlTextReader reader = new XmlTextReader(stringReader); 
            try 
            { 
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); 
                lock (locker) 
                { 
                    T item = (T)xmlSerializer.Deserialize(reader); 
                    reader.Close(); 
                    return item; 
                } 
            } 
            finally 
            { 
                if (reader != null) 
                { reader.Close(); } 
            } 
        } 
        public static T XmlDeserialize<T>(this FileInfo fileInfo) 
        { 
            string xml = string.Empty; 
            using (FileStream fs = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read)) 
            { 
                using (StreamReader sr = new StreamReader(fs)) 
                { 
                    return sr.ReadToEnd().XmlDeserialize<T>(); 
                } 
            } 
        } 

Hope this helps.

PS - The extensions came from my library on codeproject: http://www.codeproject.com/KB/dotnet/MBGExtensionsLibrary.aspx


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...