C# XmlSerializer UTF-16

            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                using (System.IO.StreamWriter writer = new System.IO.StreamWriter(ms, Encoding.GetEncoding("UTF-16")))
                {
                    new System.Xml.Serialization.XmlSerializer(typeof(ViewProcess)).Serialize(writer, vp);
                    string s = Encoding.GetEncoding("UTF-16").GetString(ms.ToArray());
                    System.Diagnostics.Debug.Print(s);
                }
            }
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.GetEncoding("UTF-16").GetBytes(s)))
                {
                    if (new System.Xml.Serialization.XmlSerializer(typeof(ViewProcess)).Deserialize(ms) is ViewProcess vp)
                    {
                        
                    }
                }

XML特定操作


            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                System.Xml.XmlWriterSettings xws = new System.Xml.XmlWriterSettings
                {
                    Indent = true,
                    OmitXmlDeclaration = true, // 去除 顶部描述 <?xml version="1.0" encoding="***" ?>
                    Encoding = Encoding.Default
                };
                using (System.Xml.XmlWriter xtw = System.Xml.XmlWriter.Create(ms, xws))
                {
                    // Namespaces (e.g 去除 xmlns:)
                    System.Xml.Serialization.XmlSerializerNamespaces np =
                        new System.Xml.Serialization.XmlSerializerNamespaces(
                            new System.Xml.XmlQualifiedName[]
                            {
                                new System.Xml.XmlQualifiedName(string.Empty, string.Empty)
                            }
                        );
                    // 序列化
                    new System.Xml.Serialization.XmlSerializer(typeof(Response)).Serialize(xtw,
                        new Response()
                        {
                            ExpiaryDate = new DateTime(2099, 12, 30),
                        }, np);
                    // 输出文本
                    string s = Encoding.Default.GetString(ms.ToArray());

                    // XML 重组
                    System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
                    xmlDocument.LoadXml(s);
                    System.Xml.XmlDocument xmlDocument2 = new System.Xml.XmlDocument();
                    var root= xmlDocument2.CreateElement("Test");
                    for (int i = 0; i < xmlDocument.ChildNodes.Count; i++)
                    {
                        var note2 = xmlDocument2.CreateElement(xmlDocument.ChildNodes[i].Name);
                        note2.InnerXml = xmlDocument.ChildNodes[i].InnerXml;
                        root.AppendChild(note2);
                    }
                    xmlDocument2.AppendChild(root);
                    // XML 写出
                    using (System.IO.MemoryStream ms2 = new System.IO.MemoryStream())
                    {
                        xmlDocument2.Save(ms2);
                        System.Diagnostics.Debug.Print(Encoding.Default.GetString(ms2.ToArray()));
                    }
                    
                }
            }

posted @ 2021-05-25 18:27  devs  阅读(554)  评论(0编辑  收藏  举报