Xelement使用C Sharp [英] Xelement using c sharp
问题描述
我想使用C Sharp使用xml进行多项选择题,我如何以这种格式创建xml
I want to use xml for a multichoice questions using c sharp, how can i create my xml in this format
<?xml version="1.0" encoding="utf-8"?>
<Total>
<Year Type="2001">
<Questions>
<Question No="1">first</Question>
<Option Type="A">ghg</Option>
<Option Type="B">hgh</Option>
<Option Type="C">ghgh</Option>
<Option Type="D">ghg</Option>
<Answer>hgh</Answer>
<Question No="2">first</Question>
<Option Type="A">ghg</Option>
<Option Type="B">hgh</Option>
<Option Type="C">ghgh</Option>
<Option Type="D">ghg</Option>
<Answer>hgh</Answer>
</Questions>
</Year>
<Year Type="2002">
<Questions>
<Question No="1">first</Question>
<Option Type="A">ghg</Option>
<Option Type="B">hh</Option>
<Option Type="C">ghgh</Option>
<Option Type="D">ghg</Option>
<Answer>hgh</Answer>
</Questions>
<Questions>
<Question No="2">second</Question>
<Option Type="A">ghg</Option>
<Option Type="B">hh</Option>
<Option Type="C">ghgh</Option>
<Option Type="D">ghg</Option>
<Answer>hgh</Answer>
</Questions>
</Year>
</Total>
请帮我...
谢谢
pls help me out...
Thanks
推荐答案
您应该在发布问题之前进行搜索或尝试以下操作:
http://www.dotnetperls.com/xelement [
You should search or try something befor posting the question:
http://www.dotnetperls.com/xelement[^]
创建一个代表您的xml的类.您可以使用VS附带的xsd.exe生成类. http://msdn.microsoft.com/en-us/library/x6c1kb0s (v = vs.80).aspx [
Create A Class representing your xml. You can generate class using xsd.exe it comes with VS. http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.80).aspx[^]
The very normal location of xsd.exe is:
C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin
C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin
您还可以使用xsdobjectgenerator获得更准确的结果:
http://www.microsoft.com/en-us/download/details.aspx? id = 7075 [ ^ ]
创建类后,您可以使用它对xml进行序列化/反序列化
You can also use xsdobjectgenerator for more accurate results:
http://www.microsoft.com/en-us/download/details.aspx?id=7075[^]
Once your class is created, you can use it serialize/deserialize your xml
public void CreateXML(Object oObject)
{
XmlDocument xmlDoc = new XmlDocument();
XmlSerializer xmlSerializer = new XmlSerializer(oObject.GetType());
using (Stream xmlStream = new FileStream("Yourpathtoxmlfile", System.IO.FileMode.OpenOrCreate))
{
xmlSerializer.Serialize(xmlStream, oObject);
xmlStream.Close();
}
}
有关序列化的示例:
http://www.jonasjohn.de/snippets/csharp/xmlserializer-example.htm [ ^ ]
An example on serialization:
http://www.jonasjohn.de/snippets/csharp/xmlserializer-example.htm[^]
这篇关于Xelement使用C Sharp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!