Home All Groups Group Topic Archive Search About

How to create an Empty XML file

Author
8 Nov 2005 1:29 PM
Gus Gustafson
I have an application that must create a new, but empty, XML file on demand. 
My original approach was to copy an existing "template XML file" into the new
XML file.  I found that I needed to pass through each table in the newly
created XML file and delete each row.  I'm wondering if there is a better
way.  For example, I can programmatically create the XML file but the problem
with this method is that the creation process is within a program.

So is there a method that uses a schema to create an empty XML file.  If
there is, where is it documented?  If not, what is suggested?

TIA

Gus

Author
8 Nov 2005 1:42 PM
S.M. Altaf [MVP]
Gus,

Is there a reason you don't want to do this from your program?  An XML file
is nothing but a text file, so you can simply create a new file with an .xml
extension, and you're done.

Unless I'm missing something from your post.

-Altaf
--------------------------------------------------------------------------------
All that glitters has a high refractive index.
www.mendhak.com


Show quote
"Gus Gustafson" <GusGustaf***@discussions.microsoft.com> wrote in message
news:4C2AC5AF-7CE7-4978-9472-61FCD3059D0B@microsoft.com...
>I have an application that must create a new, but empty, XML file on
>demand.
> My original approach was to copy an existing "template XML file" into the
> new
> XML file.  I found that I needed to pass through each table in the newly
> created XML file and delete each row.  I'm wondering if there is a better
> way.  For example, I can programmatically create the XML file but the
> problem
> with this method is that the creation process is within a program.
>
> So is there a method that uses a schema to create an empty XML file.  If
> there is, where is it documented?  If not, what is suggested?
>
> TIA
>
> Gus
>
Author
8 Nov 2005 1:59 PM
Gus Gustafson
Although I appreciate that I can create the XML file from my program, I would
prefer to define (once) the structure of the XML file in some manner and then
use that definition to create an empty XML file (with all of its tables and
attributes, only without data).


Show quote
"S.M. Altaf [MVP]" wrote:

> Gus,
>
> Is there a reason you don't want to do this from your program?  An XML file
> is nothing but a text file, so you can simply create a new file with an .xml
> extension, and you're done.
>
> Unless I'm missing something from your post.
>
> -Altaf
> --------------------------------------------------------------------------------
> All that glitters has a high refractive index.
> www.mendhak.com
>
>
> "Gus Gustafson" <GusGustaf***@discussions.microsoft.com> wrote in message
> news:4C2AC5AF-7CE7-4978-9472-61FCD3059D0B@microsoft.com...
> >I have an application that must create a new, but empty, XML file on
> >demand.
> > My original approach was to copy an existing "template XML file" into the
> > new
> > XML file.  I found that I needed to pass through each table in the newly
> > created XML file and delete each row.  I'm wondering if there is a better
> > way.  For example, I can programmatically create the XML file but the
> > problem
> > with this method is that the creation process is within a program.
> >
> > So is there a method that uses a schema to create an empty XML file.  If
> > there is, where is it documented?  If not, what is suggested?
> >
> > TIA
> >
> > Gus
> >
>
>
>
Author
8 Nov 2005 1:51 PM
Jim Hughes
Show quote
"Gus Gustafson" <GusGustaf***@discussions.microsoft.com> wrote in message
news:4C2AC5AF-7CE7-4978-9472-61FCD3059D0B@microsoft.com...
>I have an application that must create a new, but empty, XML file on
>demand.
> My original approach was to copy an existing "template XML file" into the
> new
> XML file.  I found that I needed to pass through each table in the newly
> created XML file and delete each row.  I'm wondering if there is a better
> way.  For example, I can programmatically create the XML file but the
> problem
> with this method is that the creation process is within a program.
>
> So is there a method that uses a schema to create an empty XML file.  If
> there is, where is it documented?  If not, what is suggested?

You could save the empty XML file as a embedded resource in your app and
then retrieve it and write it out as necessary.
Author
8 Nov 2005 1:59 PM
Gus Gustafson
Jim, interesting idea. Let me give some thought to that.

Thanks

Show quote
"Jim Hughes" wrote:

>
> "Gus Gustafson" <GusGustaf***@discussions.microsoft.com> wrote in message
> news:4C2AC5AF-7CE7-4978-9472-61FCD3059D0B@microsoft.com...
> >I have an application that must create a new, but empty, XML file on
> >demand.
> > My original approach was to copy an existing "template XML file" into the
> > new
> > XML file.  I found that I needed to pass through each table in the newly
> > created XML file and delete each row.  I'm wondering if there is a better
> > way.  For example, I can programmatically create the XML file but the
> > problem
> > with this method is that the creation process is within a program.
> >
> > So is there a method that uses a schema to create an empty XML file.  If
> > there is, where is it documented?  If not, what is suggested?
>
> You could save the empty XML file as a embedded resource in your app and
> then retrieve it and write it out as necessary.
>
>
>
Author
8 Nov 2005 2:44 PM
Scott M.
You could use the FillSchema method of a dataAdapter to generate a dataset
with one empty table that has all the original table's schema information
intact.  From there, you could use the dataset's WriteXMLSchema or WriteXML
methods to create the actual XML files you seek.


Show quote
"Gus Gustafson" <GusGustaf***@discussions.microsoft.com> wrote in message
news:4C2AC5AF-7CE7-4978-9472-61FCD3059D0B@microsoft.com...
>I have an application that must create a new, but empty, XML file on
>demand.
> My original approach was to copy an existing "template XML file" into the
> new
> XML file.  I found that I needed to pass through each table in the newly
> created XML file and delete each row.  I'm wondering if there is a better
> way.  For example, I can programmatically create the XML file but the
> problem
> with this method is that the creation process is within a program.
>
> So is there a method that uses a schema to create an empty XML file.  If
> there is, where is it documented?  If not, what is suggested?
>
> TIA
>
> Gus
>
Author
8 Nov 2005 2:44 PM
Cowboy (Gregory A. Beamer)
There are a variety of ways to attack this problem:

1. The template can be empty nodes, which you start populating.
2. The template can only have the important parent nodes, which you append
data to
3. You can use a DataSet format and add information that way and then
transform with XSLT and stream back out for the user
4. A schema, which is an XML file, can be transformed to become a document.

I do not know of an autogen for XML from a schema in .NET proper. I am sure
someone has solved this as a third party tool. Try sourceforge.net and see
if any open source implementations exist.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***********************************************
Think Outside the Box!
***********************************************
Show quote
"Gus Gustafson" <GusGustaf***@discussions.microsoft.com> wrote in message
news:4C2AC5AF-7CE7-4978-9472-61FCD3059D0B@microsoft.com...
>I have an application that must create a new, but empty, XML file on
>demand.
> My original approach was to copy an existing "template XML file" into the
> new
> XML file.  I found that I needed to pass through each table in the newly
> created XML file and delete each row.  I'm wondering if there is a better
> way.  For example, I can programmatically create the XML file but the
> problem
> with this method is that the creation process is within a program.
>
> So is there a method that uses a schema to create an empty XML file.  If
> there is, where is it documented?  If not, what is suggested?
>
> TIA
>
> Gus
>
Author
8 Nov 2005 3:39 PM
Gus Gustafson
Hey, folks thanks for your thoughts. Here's what I came up with as a result
of your inputs.

1.  I created an XSD file that defines the XML file. It is currently in a
directory that is relative to the location of the executable. I do like Jim's
idea (make it a resource) and I'm still thinking about it.

2.  When I need to create a new instance of the XML file, I take the
following steps:

    a.  data_set = new DataSet ( ) ;
    b.  data_set.ReadXmlSchema ( XDS_path ) ;
    c.  directory = Path.GetDirectoryName ( XML_path ) ;
         if ( ! Directory.Exists ( directory ) )
            {
            Directory.CreateDirectory ( directory ) ;
            }
    d.  data_set.WriteXmlSchema ( XML_path ) ;

3.  Massage the newly created XML file using ReadXml and WriteXml.

The only thing that I needed to remember (and forgot) was to check
table.Rows.Count before trying to perform

    DataRow  row = table.Rows [ 0 ] ;

Thought I'd pass it on.

Thanks for everyones help.

Regards,

Gus
Author
9 Nov 2005 4:26 PM
Gus Gustafson
One additional item.

When reading the XML file into a data set, use one of the ReadXML overloads
that includes the ReadXmlMode parameter set to ReadSchema. For example,

            data_set.ReadXml (
                path,
                XmlReadMode.ReadSchema ) ;

Likewise, when writing to the XML file from the data set, use one of the
WriteXML overloads that includes the WriteXmlMode parameter set to
WriteSchema. For example,

            data_set.WriteXml (
                path,
                XmlWriteMode.WriteSchema ) ;


Again, thanks to all for the help.

Gus

AddThis Social Bookmark Button