使用C#编写XML文件 [英] Writing an XML file using C#
本文介绍了使用C#编写XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以帮助我编写一个小的函数(不超过10行)以使用C#编写以下xml(文件名1.xml)吗?
Can some one help me with writing a small function (not more than 10 lines ) for writing the following xml (file name 1.xml) using C#?
<items>
<item id="test"/>
</item>
</items>
推荐答案
最简单的方法是使用File.WriteAllText
:
请参阅 http://msdn.microsoft.com/en-us/library/ms143375.aspx [ ^ ]
示例:
Easiest way would be usingFile.WriteAllText
:
See http://msdn.microsoft.com/en-us/library/ms143375.aspx[^]
Example:
string xml = @"<items>
<item id="test"/>
</item>
</items>";
File.WriteAllText(@"d:\files\1.xml", xml);
这取决于您实际要实现的目标,例如,如果要将对象存储为文本文件,则可以使用序列化,Google for C# XML序列化,您将获得很多示例.
It depends what you are actually trying to achieve, for example if you want to store an object as a text file you can use serialization, google for C# XML Serialization and you''ll get lots of examples..
printf("%s", "1.xml");
如果您要编写内容,那么周围有各种各样的帮助程序库.
If you mean writing the contents then there are various helper libraries around to do it for you.
这篇关于使用C#编写XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文