I'm new for c# and XML. I have xsd file and created cs file from it like described here Generating XML file using XSD file Now I need to generate XML file that contains multiple invoices and every invoice has 1 or more subrows. How to do that?
Asked
Active
Viewed 289 times
-1
-
What are you tried? – tym32167 Nov 22 '16 at 13:43
-
Try to google it, it can help. – mybirthname Nov 22 '16 at 13:43
-
I did google, closest example is probably here https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx But there looks like only one OrderItem. But if I have many? – Rein Kannumäe Nov 22 '16 at 13:48
-
Example is typeof(MyClass) you can changed this in typeof(List
) and voilla – lordkain Nov 22 '16 at 13:50
1 Answers
0
1 start VS command prompt
2 generate classes, based on XSD with SvcUtil.exe
svcutil xml_in_arved.xsd /language:C# /dataContractOnly /importxmltypes /out:class.cs
3 write code to fill classes
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book>" +
" <title>Oberon's Legacy</title>" +
" <price>5.95</price>" +
"</book>");
invoices invoices = new invoices();
invoices.Nodes = new XmlNode[2];
invoices.Nodes[0] = doc.CreateNode("element", "test", "myNamespace");
invoices.Nodes[1] = doc.CreateNode("element", "tes2", "myNamespace");
4 serialize classes to xml
Serialize example
var content = invoices;
var filename = @"c:\temp\xmlserialise.xml";
using (TextWriter textWriter = new StreamWriter(filename, false))
{
var serializer = new XmlSerializer(content.GetType());
serializer.Serialize(textWriter, content);
}

lordkain
- 3,061
- 1
- 13
- 18
-
I'm using this xsd http://wiki.directo.ee/_media/et/xml_in_arved.xsd Generated classes. How to fill classes? – Rein Kannumäe Nov 22 '16 at 14:06
-
OK, probably this is right answer, I just don't know how to use it. Like I posted, I'm new to c#. First I generated classes like this xsd.exe /classes xml_in_arved.xsd and it gave me bigger cs file than using svcutil xml_in_arved.xsd /language:C# /dataContractOnly /importxmltypes /out:class.cs Not sure which one is right. – Rein Kannumäe Nov 23 '16 at 06:53
-
look at the difference what is generated and what fits your need. Our job is also some reseach and exploring. – lordkain Nov 23 '16 at 08:16