c# xml序列化和反序列化。也就是xml的解析和反解析。
用习惯了newTownSoft.json 的json反序列化。碰到xml是真的不习惯。
每次json反序列化都是直接把json丢到bejson网站生成一个实体类,稍微修改修改一点点变量名。然后直接newTownSoft反序列化,一下就得到一个实体类了。今天调某个接口,碰到xml。
记录如下。
xml
<response>
<functionID>setItemsPics</functionID>
<time>2017-09-07 15:51:04</time>
<ItemsIDList>
<itemIDInfo>
<operCode>0</operCode>
<operation>操作成功</operation>
</itemIDInfo>
<itemIDInfo>
<itemID>1125507057</itemID>
<operCode>0</operCode>
<operation>操作成功</operation>
</itemIDInfo>
</ItemsIDList>
</response>
实体类如下:先引用 using System.Xml.Serialization;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Serialization; namespace DDTopManager.Response { [XmlRoot( "response" )] public class RSPItemPicSet { //操作返回xml示例,按这个xml格式定义可序列化的实体类 // public string str = @"<response> // <functionID>setItemsPics</functionID> // <time>2017-09-07 15:51:04</time> // <ItemsIDList> // <itemIDInfo> // <itemID>1125550957</itemID> // <operCode>0</operCode> // <operation>操作成功</operation> // </itemIDInfo> // <itemIDInfo> // <itemID>1125507057</itemID> // <operCode>0</operCode> // <operation>操作成功</operation> // </itemIDInfo> // </ItemsIDList> //</response>"; [XmlElement( "functionID" )] public string functionID { get ; set ; } [XmlElement( "time" )] public string time { get ; set ; } [XmlArray( "ItemsIDList" ), XmlArrayItem( "itemIDInfo" )] public List<itemIDInfo> ItemsIDList { get ; set ; } [XmlRoot( "itemIDInfo" )] public class itemIDInfo { [XmlElement( "itemID" )] public string itemID { get ; set ; } [XmlElement( "operCode" )] public string operCode { get ; set ; } [XmlElement( "operation" )] public string operation { get ; set ; } } } } |
引用(这是从str这个xml中反序列化到实体类)大致如下:
1 2 3 4 5 | XmlSerializer se = new XmlSerializer( typeof (RSPOrdersListGet)); byte [] arry = Encoding.UTF8.GetBytes(str); System.IO.Stream me = new System.IO.MemoryStream(arry); RSPOrdersListGet rsp = (RSPOrdersListGet)se.Deserialize(me); |
序列化如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | RSPOrdersListGet RSPLG = new RSPOrdersListGet(); RSPLG.functionID = "setItemsPics" ; List<itemIDInfo> itemidinfos = new List<itemIDInfo>(); itemIDInfo idi = new itemIDInfo(); idi.operation = "000" ; idi.operCode = "0011" ; idi.itemId = "aaa" ; itemidinfos.Add(idi); itemIDInfo idi1 = new itemIDInfo(); idi1.operation = "000" ; idi1.operCode = "0011" ; idi1.itemId = "bbb" ; itemidinfos.Add(idi1); RSPLG.ItemsIDList = itemidinfos; XmlSerializer rSPOrdersListGet = new XmlSerializer( typeof (RSPOrdersListGet)); rSPOrdersListGet.Serialize(Console.Out, RSPLG); |
序列化的时候改命名空间编码等
1 2 3 4 5 6 7 8 9 10 11 12 13 | XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true ; settings.IndentChars = " " ; settings.NewLineChars = "\r\n" ; settings.Encoding = Encoding.GetEncoding( "gbk" ); using (XmlWriter xmlWriter = XmlWriter.Create(sm, settings)) { // 强制指定命名空间,覆盖默认的命名空间 XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(); namespaces.Add( string .Empty, string .Empty); rSPOrdersListGet.Serialize(xmlWriter, RSTGS, namespaces); xmlWriter.Close(); }; |
记录,以免忘记。
有一点和newTownSoft.json序列化和反序列化不同的是,比如数字List<Entity> MyData 这个MyData 和json要对的上,
这里的直接用 头部的约束来定义即可
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构