Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to use xml for a multichoice questions using c sharp, how can i create my xml in this format

XML
<?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
Posted

You should search or try something befor posting the question:
http://www.dotnetperls.com/xelement[^]
 
Share this answer
 
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:
SQL
C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin
C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin


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

C#
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();

  }
} 


An example on serialization:
http://www.jonasjohn.de/snippets/csharp/xmlserializer-example.htm[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900