In the previous blog, I introduced the xml serialization principles. In the basic tutorial of wcf, Contract data Contract DataContract serialization predecessor XmlSerializer xml serialization. Today we are moving forward along the last description, and this time there may be very few content, however, it should be said that if we want to further control xml, it is still necessary. Well, let's not talk about it. xml Serialization uses XmlSerializer in. Net, which is in the namespace of System. Xml. Serialization. XmlSerializer. The last time is only the default serialization rule used by XmlSerializer. In many cases, we need to control the output of xml, so we need human intervention and control.
In the xml serialization process, if we define A, A inherits from B, but if we serialize through B, it cannot be applied to. What does this mean? That is to say, if we use B as the type during serialization, we must also deserialize it to B instead of A during deserialization. This is a bit complicated and requires us to understand it. I don't understand it very well.
In the previous blog, XmlElement in the generated xml is only a simple attribute name. What should we do if we want to add a namespace for the attribute name? Code and truth
In fact, the most important thing is XmlAttributeOverrides and XmlAttributes to control the output of xml. Now we can implement our needs.
However, we use the declarative method to solve this problem,
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. xml; 6 using System. xml. serialization; 7 using System. diagnostics; 8 9 namespace Chinaer. wcfDemo. consoleClient 10 {11 class Program 12 {13 static void Main (string [] args) 14 {15 // Person person = new Person (25, Guid. newGuid () 16 // {17 // Date = DateTime. now 18 //}; 19 // person. userName = "guozhiqi"; 20 // person. userPwd = "123"; 21 // Serialize <Person> (person, "person. xml "); 22 Person person = new Person () 23 {24 Age = 12, 25 UserPwd =" 21 ", 26 UserName =" guo Zhiqi ", 27 Date = DateTime. now 28}; 29 30 using (XmlWriter writer = new XmlTextWriter ("person. xml ", Encoding. UTF8) 31 {32 XmlSerializer serializer = new XmlSerializer (typeof (Person); 33 serializer. serialize (writer, person); 34} 35 36 Process. start ("person. xml "); 37 Console. read (); 38} 39 40 // <summary> 41 // serialization Method 42 // </summary> 43 // <typeparam name = "T"> </typeparam> 44 // <param name = "instace"> </param> 45 // <param name = "fileName"> </param> 46 public static void Serialize <T> (T instace, string fileName) 47 {48 using (XmlWriter writer = new XmlTextWriter (fileName, Encoding. UTF8) 49 {50 XmlSerializer serializer = new XmlSerializer (typeof (T); 51 serializer. serialize (writer, instace); 52} 53 Process. start (fileName); 54} 55} 56 [XmlRoot (ElementName = "guozhiqi", Namespace = "http://www.guozhiqi.com")] 57 // <summary> 58 // define an object class Person 59 // </summary> 60 public class Person 61 {62 private Guid _ id; 63 64 private DateTime _ date; 65 // note that we do not have the default constructor 66 internal double Age {get; set ;} // Private field age 67 /// <summary> 68 /// serialize to xml Attribute 69 through XmlAttributeAttribute /// </summary> 70 [XmlAttribute (AttributeName = "GuidID ", namespace = "http: // guidID")] 71 public Guid ID 72 {73 get {return _ id;} 74 set 75 {76 _ id = value; 77} 78} // public random number 79 [XmlElement (ElementName = "DateTime", Namespace = "http: // date")] 80 public DateTime Date 81 {82 set 83 {84 _ date = value; 85} 86 get 87 {88 return _ date; 89} 90} 91 92 public string UserName {get; set;} 93 94 public string UserPwd {get; set;} 95 public Person () {} 96 public Person (double age, Guid id) 97 {98 this. age = age; 99 100} 101} 102 103}
Pay attention to the above Code. We will first post the generated xml, which is easier to compare.
1 <? Xml version = "1.0" encoding = "UTF-8"?>
<Guozhiqi xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: xsd = "http://www.w3.org/2001/XMLSchema"
D1p1: GuidID = "00000000-0000-0000-0000-000000000000"
Xmlns: d1p1 = "http: // guidID" xmlns = "http://www.guozhiqi.com">
<DateTime xmlns = "http: // date"> 2013-03-21T23: 31: 13.8584896 + 08: 00 </DateTime>
<UserName> Guo Zhiqi </UserName> <UserPwd> 21 </UserPwd> </guozhiqi>
We can see that the XmlAttributeAttribute feature is used to serialize a field or attribute to an Xml attribute, and the name (alias) or namespace can be specified.
You can use XmlElementAttribute to serialize a field or attribute to an Xml element. You can specify Name, NameSpace, and Order sorting. You can search for the differences between xml attributes and metadata. The difference is very small.
It is very easy to control the output of xml structures through declaration, but programming is not very difficult, because today I have been writing two blogs, and I have been writing for nearly two hours. Direct call programmers cannot afford to hurt
If you don't force yourself, you don't know how good you are.