A quick way to read and write xml

  Handling of a variety of xml. E.g:

  1 can be used directly in the .NET XmlDocument xml document operation involving XmlDocument, the XmlTextReader, using XmlElement like object.

  2 can be directly xml as a string, and then processed using regular expressions.

  The first approach leads to xml code structure like the one set of xml layer, unattractive (LINQ process employed would be better); The second embodiment is useful in extracting certain elements xml, there are limitations. In fact, the process can use XML serialization, deserialization mechanism for processing.

 

  Since .NET class object can be serialized into the xml file can be deserialized into an object class from xml. Based on this idea, a valid xml processing can be divided into the following steps:

  1. generation of xml schema file

  2. Based on the xml schema file structure corresponding to a class file, such as a class name XmlMock

  3. Using the constructed class XmlMock, deserialized from xml file, initializing the object XmlMock

  4. XmlMock object fields process (add, delete, change)

  Reference 2: http://msdn.microsoft.com/zh-cn/library/x6c1kb0s(v=vs.80).aspx

  The main use xsd.exe (if x64 operating system, C: \ Program Files into C: \ Program Files (x86))

      C: \ Program Files \ Microsoft SDKs \ Windows \ v7.0A \ Bin \ xsd.exe 或

    C: \ Program Files \ Microsoft SDKs \ Windows \ v6.0A \ Bin \ xsd.exe

  Simple use

  ------------------------------------

  cd C:\Program Files\Microsoft SDKs\Windows\v7.0A\Bin

  xsd.exe test1.xml

  xsd.exe test1.xsd /classes

  ------------------------------------

  The above two steps, you can generate test1.cs file, xml file the class name is the name of the root node.

  Xml file corresponding to the class files to obtain the rest is xml serialization, the deserialization.

 1         /// <summary>
2 /// Convert serializable object to XML.
3 /// </summary>
4 public static string GetSerializedContent<T>(T xmlObj) where T : class
5 {
6 XmlSerializer serializer = new XmlSerializer(typeof(T));
7 MemoryStream stream = new MemoryStream();
8
9 serializer.Serialize(stream, xmlObj);
10 string xmlContent = string.Empty;
11 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
12 {
13 stream.Position = 0;
14 xmlContent = reader.ReadToEnd();
15 }
16
17 return xmlContent;
18 }
19
20 /// <summary>
21 /// deserialize xml content to xmlObj
22 /// </summary>
23 public static object DeserializeObject(Type ObjectType, string ObjectContent)
24 {
25 XmlSerializer serializer = new XmlSerializer(ObjectType);
26 byte[] buffer = Encoding.UTF8.GetBytes(ObjectContent);
27
28 MemoryStream stream = new MemoryStream(buffer);
29 return serializer.Deserialize(stream);
30 }

  The above-described two methods to complete serialization and deserialization of xml.

Reproduced in: https: //www.cnblogs.com/ahomer/archive/2012/03/26/2418139.html

Guess you like

Origin blog.csdn.net/weixin_34414196/article/details/94005796