下面讲述根据xml生成对应序列化反序列化类的过程,xml需要首先转化为xsd,然后再生成为实体类。其中,XSD是XML Schema Definition的缩写。

1.拥有一个xml文件

2.打开vs的command line:

xsd *.xml ,执行这个命令 是通过xml文件生成xsd文件,xsd文件将生成在当前命令目录下。

xsd *.xsd /classes 是通过xsd文件生成xml的解析类,cs文件。示例顺便指定了命名空间为Test

这样就可以把cs文件放到项目中去使用了,这个cs文件就是xml对应的解析类。

PS :xsd自动解析的文件不知道每个节点是不是唯一的还是有多个。如果需要指定某节点是唯一的,可以在生成xsd文件之后,把xsd文件放到vs中,

使用xml(Text) editor 方式打开,然后指定maxOccurs="1",这样就指定了当前节点为唯一一个了。maxOccurs="unbounded",表明xml节点可以有很多个。 这样再把修改的xsd使用xsd *.xsd /classes 命令生成cs文件,或许更满足你的需求。

3. 解析 xml

        static void Main(string[] args)
{
try
{
XmlSerializer ser = new XmlSerializer(typeof(Response));
//读取xml进行解析
FileStream fs = new FileStream(@"D:\a.xml", FileMode.Open);
Response response = ser.Deserialize(fs) as Response;
response.SIP.Line1.Port = "9999";
fs.Close();
//修改xml并保存
fs = new FileStream(@"D:\a.xml", FileMode.Open, FileAccess.Write);
ser.Serialize(fs, response);
fs.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

  其中Response是我xml的首节点。

附加:

我的xml示例是:

<Response>
<APIVersion>1.0</APIVersion>
<Authenticated>true</Authenticated>
<Global>
<SoftwareUpgrade>
<Enabled>false</Enabled>
<Version/>
<URL/>
<Rule/>
<RetryInterval>3600</RetryInterval>
</SoftwareUpgrade>
<Provisioning>
<Enabled>true</Enabled>
<URL>https://eeee.cn</URL>
<Interval>86400</Interval>
</Provisioning>
<Phonebook>
<Enabled>true</Enabled>
<URL>https://ffff.com</URL>
</Phonebook>
</Global>
<SIP>
<Line1>
<DisplayName>Terr</DisplayName>
<URI>5538769</URI>
<PreferredURI>for@abc.com</PreferredURI>
<AuthName>5530769</AuthName>
<Password>1sPU</Password>
<Transport>TCP</Transport>
<Mailbox>
123
</Mailbox>
<Port>7899</Port>
<Proxy>
<AutoMode>off</AutoMode>
<Proxy1>proxy</Proxy1>
<Proxy2/>
</Proxy>
</Line1>
</SIP>
</Response>

  

最新文章

  1. C语言中的循环结构与选择结构
  2. 【AngularJS】—— 4 表达式
  3. 异步get请求之代理方法
  4. Constructing Roads (MST)
  5. Jquery 表格操作,记录分页情况下,每一页中被用户勾选的信息
  6. JavaScript 的字符串转换
  7. HTML&amp;CSS基础学习笔记1.32-选择器是什么
  8. Fiddler使用笔记
  9. C++指针的操作和运算(转)
  10. Spring Tool Suit 在Eclipse上的安装
  11. Unity3D 中材质球(Material)预制体打包成AB(AssetBundle)出现材质丢失问题的解决方案
  12. 如何开发自己的搜索帝国之安装ik分词器
  13. MVC和MVP设计模式
  14. vsftpd 的端口模式以及端口映射问题
  15. Minimum Spanning Trees
  16. OJ每日一练试水第1天,聊聊输入输出问题
  17. pip install 安装出现问题:UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode characters in position XX的解决办法
  18. JAVA二分搜索树
  19. -bash: xhost: command not found
  20. VisualVM + BTrace

热门文章

  1. [转]SSM(Spring+SpringMVC+Mybatis)框架搭建详细教程【附源代码Demo】
  2. 导入导出sql结构和数据
  3. pca算法实现
  4. Python 函数与内置函数
  5. day 68 Django基础四之模板系统
  6. Mybatis Resultmap 简化之超级父类
  7. LeetCode第一题—— Two Sum(寻找两数,要求和为target)
  8. UVA - 11327
  9. [JZOJ4769]【GDOI2017模拟9.9】graph
  10. JVM的内存空间