Can an XmlDocument file be created from an XSD file?

R

Ray Stevens

I'm not an expert on XML so have a question I have been unable to find an
answer. I have a fairly complex XSD file that was provided to me, for which
I used XSD.exe to generate a class. I added both the class and xsd files to
a C# VS 2005 project (Not Web Site). I need to create an XmlDocument that
will be populated with values and passed to a Web Service, where additional
elements will be populated and returned. I am at a complete loss as to how
to create the document from the class or xsd file.

For example, I tried the following:

LXML lxml = new LXML(); // NOTE: LXML is the class genererated from the
XSD file
XmlDocument doc = new XmlDocument();
doc.LoadXml(lxml.ToString());

It compiles, but chokes at runtime... looking nothing like an XML document.

Anyone know how this can be done... or IF it can be done?
 
N

Nicholas Paldino [.NET/C# MVP]

Ray,

Well, are you asking if you want an instance of XML that corresponds to
the schema, or can you take the schema and put it into an XML document? If
the answer is the latter, then you can load the schema as an XML document,
since XML Schemas ARE XML.

However, if you are looking to create an instance of the schema, that is
kind of hard. That's like saying give me an int (in .NET terms). You could
give them any value that falls in an int, but what value do you want?

Also, given that XML Schemas allow for elements of any type (i.e. no
constraints), then what do you possibly do there?

Hope this helps.
 
G

Goran Sliskovic

....
For example, I tried the following:

LXML lxml = new LXML(); // NOTE: LXML is the class genererated from the
XSD file
XmlDocument doc = new XmlDocument();
doc.LoadXml(lxml.ToString());

It compiles, but chokes at runtime... looking nothing like an XML
document.
....

You have to use XmlSerializer class to serialize it to XML. ToString() does
not return xml string (it might if you override it, but it is not doing it
by default).

Regards,
Goran
 
N

Nick Hounsome

Ray Stevens said:
I'm not an expert on XML so have a question I have been unable to find an
answer. I have a fairly complex XSD file that was provided to me, for
which I used XSD.exe to generate a class. I added both the class and xsd
files to a C# VS 2005 project (Not Web Site). I need to create an
XmlDocument that will be populated with values and passed to a Web
Service, where additional elements will be populated and returned. I am at
a complete loss as to how to create the document from the class or xsd
file.

For example, I tried the following:

LXML lxml = new LXML(); // NOTE: LXML is the class genererated from the
XSD file
XmlDocument doc = new XmlDocument();
doc.LoadXml(lxml.ToString());

It compiles, but chokes at runtime... looking nothing like an XML
document.

Anyone know how this can be done... or IF it can be done?

See XmlSerializer - it will serialize/deserialize the object lxml as a
stream of characters ("<xml .....").

XmlSerializer can serialize pretty well anything as XML.
The purpose of xsd.exe is to put all the right attributes and stuff in LXML
to make XmlSerializer serialize it in the form specified by the XSD file.

You do not need XmlDocument at all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top