在WPF中使用C#编写Xml文件 [英] Writing Xml File using C# in WPF

查看:326
本文介绍了在WPF中使用C#编写Xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
这可能很简单,但是我很困惑,我想写Xml文件
例如:

Hello,
This may be simple, but iam confused, i want to write Xml file
for eg:

<sports>
<racing>
<tournament totalrounds="5" cars="2" />
</racing>
<start>
<car name="car1" direction="Left" roundscount="3">
<round begin="100" end="1000" />
<round begin="100" end="500" />
<round begin="500" end="1500" />
</car>
<car name="car2" direction="Right" roundscount="2">
<round begin="1000" end="100">
<round begin="1500" end="500">
</round></round></car>
</start>
</sports>



在这里,Totalrounds = 5是car1的3轮和car2的2轮.
不.的汽车= 2,这些值是动态的.
我想循环运行,因为如果有例如:说100轮和10辆车.它应该相应地编写xml.
该车的回合计数应该是动态变化的,例如3车6轮意味着每辆车的回合计数= 2,即,它应为每辆车随机显示,并且还应指示方向将一辆车向左移至另一辆车再向右移,然后再向左移..

可以得到任何帮助.



Here Totalrounds=5 is 3 rounds of car1 and 2 rounds of car2.
no. of cars =2 these values are dynamic.
i want to run in a loop because if there are eg: say 100 rounds and 10 cars.it should write the xml accordingly.
Roundscount should come dynamically for that car say 3 cars 6 rounds means roundscount=2 for each car i.e, it should show randomly for each car and also direction one car left other car right again next car directio left..

any help can be appreciated.

推荐答案

确定要使用已经设计好的模式来编写XML文件吗?您可以避免手动编程XML,而让序列化程序为您完成.

我能提供的最好建议是使用数据合同.请参阅 http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ].

请参阅我过去提倡的使用这种方法的答案:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [
Are you sure you need to write the XML file using exactly the schema which you already devised? You can avoid programming XML manually and let a serializer do it for you.

The best I can advise is using Data Contract. See http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Please see my past answers I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating a property files...[^].

If you really need to use lower-level XML read/write methods, here is what you got:


  1. 使用System.Xml.XmlDocument类.它实现了DOM接口;如果文档太大,则这种方法最简单,也足够好.
    请参见
    http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx [ http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [http://msdn.microsoft.com/en-us/library/bb387063.aspx [

  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



—SA


这篇关于在WPF中使用C#编写Xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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