在xml文件中添加更多部分 [英] Adding More sections in an xml file

查看:90
本文介绍了在xml文件中添加更多部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在创建一个xml文件来保存GUI值。我使用configurationManager和appSettings方法来创建文件。我能够创建如下的xml文件



 <  配置 >  
< appsettings >
< add key = A1 = 10000 / >
< add key = A2 value = 10000 / >
< add key = A3 value = 10000 / >
......
< / appsettings >
< / configuration >





但是我想在xml中添加如下所示的更清晰的部分

 <   configa  >  
<! - Config1 - >
< add key = A1 = 10000 / >
<! - Config2 - >
< add key = A1 value = < span class =code-keyword> 10000 / >
<! - Config3 - >
< add key = A1 value = 10000 / >
< / configa >
< < span class =code-leadattribute> configb >
<! - ConfigB1 - >
< add key = B1 < span class =code-attribute> value = 10000 / > ;
<! - ConfigB2 - >
< add key = B1 value = 10000 / >
<! - < span class =code-keyword> ConfigB3 - >
< add key = B1 value = 10000 / >
< / configb >





如何实现这一目标?请建议。

解决方案

最好不要直接执行XML操作值,而是使用序列化https://en.wikipedia.org/wiki/Serialization [ ^ ]。



最强大,最先进且最易于使用的序列化基于数据合同 http://msdn.microsoft.com/en-us/library/ms733127。 aspx [ ^ ]。



详情请见我过去的答案:

如何将数据合同从C#/ WCF代码传递给托管C ++ [ ^ ],

如何利用XML文件编写器和阅读器在我的表单申请中? [ ^ ],

创建属性文件...... [ ^ ]。



如果您仍需要保持较低级别,.NET FCL提供了不同的解析方式和生成XML。这是我对它们的简短概述:

  1. 使用 System.Xml.XmlDocument 类。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx
  2. 使用类 System.Xml .XmlTextWriter System.Xml.XmlTextReader ;这是最快的阅读方式,特别是你需要跳过一些数据。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx
  3. 使用类 System.Xml.Linq.XDocument ;这是类似于 XmlDocument 的最合适的方式,支持LINQ to XML Programming。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx http://msdn.microsoft.com/en-us/library/bb387063.aspx




  4. -SA


Hi,

I am creating an xml file to save the GUI values. I used the configurationManager and appSettings approach to create the file. I am able to create the xml file as below

<configuration>
	<appsettings>
		<add key="A1" value="10000" />
                <add key="A2" value="10000" />
                <add key="A3" value="10000" />
......
	</appsettings>
</configuration>



But I want to add more clear sections like as below in the xml

<configa> 
            <!-- Config1 -->
		<add key="A1" value="10000" />
            <!-- Config2 -->
		<add key="A1" value="10000" />
            <!-- Config3 -->
		<add key="A1" value="10000" />
</configa>
<configb> 
            <!-- ConfigB1 -->
		<add key="B1" value="10000" />
            <!-- ConfigB2 -->
		<add key="B1" value="10000" />
            <!-- ConfigB3 -->
		<add key="B1" value="10000" />
</configb>



How do I achieve this? Please suggest.

解决方案

Better don't do XML manipulation values directly, use serialization instead: https://en.wikipedia.org/wiki/Serialization[^].

Most robust, advanced and yet easiest to use serialization is based on Data Contract: http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

For further detail, please see my past answers:
how to pass a datacontract from a C#/WCF code to Managed C++[^],
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

If you still need to stay on lower level, .NET FCL offers different ways for parsing and generating XML. This is my short overview of them:

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx.
  2. Use the classes System.Xml.XmlTextWriter and System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx, http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx.
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx, http://msdn.microsoft.com/en-us/library/bb387063.aspx.



—SA


这篇关于在xml文件中添加更多部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆