471,086 Members | 894 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,086 software developers and data experts.

Constructing XML file based on Schema

Sek
Hi Folks,

I would like to create a XML file that adhere to the Schema definition
files.

My schema definition files import/include further XSD files and
internal references can be to a depth of two or three.

The data required for the creation of XML are present in the form of
user defined data structures.

What would be the ideal way to achieve this?

TIA.
Sek

Sep 17 '06 #1
5 2159


Sek wrote:

I would like to create a XML file that adhere to the Schema definition
files.

My schema definition files import/include further XSD files and
internal references can be to a depth of two or three.
With .NET 2.0 you can build an XmlSchemaSet
<http://msdn2.microsoft.com/en-us/library/system.xml.schema.xmlschemaset.aspx>
which gives a logical view on the compilation of all schemas and you can
then walk the schema object model
<http://blogs.msdn.com/stan_kitsis/archive/2005/08/06/448572.aspxand
create elements and attributes as needed.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 17 '06 #2
Sek
Thanks Martin.

Is there any way to achieve this in 1.1?
Martin Honnen wrote:
Sek wrote:

I would like to create a XML file that adhere to the Schema definition
files.

My schema definition files import/include further XSD files and
internal references can be to a depth of two or three.

With .NET 2.0 you can build an XmlSchemaSet
<http://msdn2.microsoft.com/en-us/library/system.xml.schema.xmlschemaset.aspx>
which gives a logical view on the compilation of all schemas and you can
then walk the schema object model
<http://blogs.msdn.com/stan_kitsis/archive/2005/08/06/448572.aspxand
create elements and attributes as needed.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 17 '06 #3


Sek wrote:

Is there any way to achieve this in 1.1?
..NET 1.x also has a SOM (Schema Object Model) but slightly different, see
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconXSDSchemaObjectModelSOM.asp>
for details.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 17 '06 #4
Sek
Martin,

I did reference some of those topics, but may not be to fullest extent.

I have advanced to an extent in reading the contents of schema files.
In brief, the schema files i am using are nested, in the sense, i have
the following:
-Parent.xsd [Schema directly used by the xml file]
-Child1.xsd, Child2.xsd [Both the schemas are import/included by the
Parent.xsd]
-Types1.xsd, Types2.xsd [Contains some basic types import/included by
the Childx.xsd files]

Basically, the schema is not so complicated, but i have issues
pertaining to nillable types, wherein i have to use "xsi:nil=true" for
emplty nillable type elements. My approach is to parse through the
schema recursively and get the list of type declaration for
XmlSchemaElements.
Then for every empty element to be written to xml file, i check the
above list for nillable type, in which case i add "xsi:nil=true" to the
element.

Following is the code for obtaining list of all xmlschemaelements
recursively:

-----BEGIN-----
XmlSchemaCollection o = new XmlSchemaCollection();
o.Add(sIssueType,new XmlTextReader(sSchemaFile),new XmlUrlResolver());
_oCollecElements = new ArrayList();

foreach(XmlSchema ienmr in o)
{
foreach(object item in ienmr.Elements.Values)
{
_oCollecElements.Add(item);
}
}
--------END------

The above approach lists all the xmschemaelements from Parent.xsd,
Child1.xsd and Child2.xsd , but doesn't list the xmlschemaelements in
Types1.xsd and Types2.xsd. The Types1.xsd is imported by Child1.xsd
using the following statement <xs:include schemaLocation="Types1.xsd"
/within the Child1.xsd file. So, i am expecting the
XmlSchemaCollection to resolve the types in the Types1.xsd file as
well.

Does my approach make any sense to you?

TIA.
Sek

Martin Honnen wrote:
Sek wrote:

Is there any way to achieve this in 1.1?

.NET 1.x also has a SOM (Schema Object Model) but slightly different, see
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconXSDSchemaObjectModelSOM.asp>
for details.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 17 '06 #5
"Sek" <se****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Martin,

I did reference some of those topics, but may not be to fullest extent.

I have advanced to an extent in reading the contents of schema files.
In brief, the schema files i am using are nested, in the sense, i have
the following:
-Parent.xsd [Schema directly used by the xml file]
-Child1.xsd, Child2.xsd [Both the schemas are import/included by the
Parent.xsd]
-Types1.xsd, Types2.xsd [Contains some basic types import/included by
the Childx.xsd files]

Basically, the schema is not so complicated, but i have issues
pertaining to nillable types, wherein i have to use "xsi:nil=true" for
emplty nillable type elements. My approach is to parse through the
schema recursively and get the list of type declaration for
XmlSchemaElements.
Then for every empty element to be written to xml file, i check the
above list for nillable type, in which case i add "xsi:nil=true" to the
element.

Following is the code for obtaining list of all xmlschemaelements
recursively:

-----BEGIN-----
XmlSchemaCollection o = new XmlSchemaCollection();
o.Add(sIssueType,new XmlTextReader(sSchemaFile),new XmlUrlResolver());
_oCollecElements = new ArrayList();

foreach(XmlSchema ienmr in o)
{
foreach(object item in ienmr.Elements.Values)
{
_oCollecElements.Add(item);
}
}
--------END------

The above approach lists all the xmschemaelements from Parent.xsd,
Child1.xsd and Child2.xsd , but doesn't list the xmlschemaelements in
Types1.xsd and Types2.xsd. The Types1.xsd is imported by Child1.xsd
using the following statement <xs:include schemaLocation="Types1.xsd"
/within the Child1.xsd file. So, i am expecting the
XmlSchemaCollection to resolve the types in the Types1.xsd file as
well.

Does my approach make any sense to you?
Yes, but you would need to recursively walk the included / imported schemas
using XmlSchema.Includes:

foreach (XmlSchemaImport included in schema.Includes)
{
if (included.Schema != null)
{
Recurse(included.Schema);
}
}

John
Sep 17 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Vitor Rodrigues | last post: by
1 post views Thread by Scott Brady Drummonds | last post: by
reply views Thread by Zbyszek Cybulski | last post: by
1 post views Thread by Jiho Han | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.