sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Ferrari, Eduardo's Avatar

Problem creating XML file...


Question posted by: Ferrari, Eduardo (Guest) on November 12th, 2005 05:08 AM
Hi all!

I'm trying to create this XML file:

<?xml version="1.0" encoding="utf-8" ?>
<Build type="Daily" sync="True" compile="True" assemble="True" >

<Sync version="1.0.0.0" branch="QA">
<Directories basePath="//OpicsPlus/Source/Release" />
</Sync>
</Build>

For this, I'm trying to use the code below:

private static BuildConfiguration SaveXML(XmlDocument xml, string fileName)
{
// Creates the nodelist
XmlNodeList nodeList;
XmlElement user = xml.DocumentElement;
nodeList = user.SelectNodes("//Build");

// Clear all the node
user.RemoveAll();
xml.Save(fileName);

// Creates a new element for the build configuration
XmlElement xmlBuild = xml.CreateElement("//Build");
xml.DocumentElement.AppendChild(xmlBuild);

// Navigates to this node
xml.SelectSingleNode("//Build");

xml.CreateAttribute("type");
xml.CreateAttribute("sync");
xml.CreateAttribute("compile");
xml.CreateAttribute("assemble");

// Set the attribute values for the build configuration
xmlBuild.SetAttribute("type", BuildType);
xmlBuild.SetAttribute("sync", BuildSync.ToString());
xmlBuild.SetAttribute("compile", BuildCompile.ToString());
xmlBuild.SetAttribute("assemble", BuildAssemble.ToString());

// Navigates to this node
xml.SelectSingleNode("//Build");

// Creates a new element for the sync configuration
XmlElement xmlSync = xml.CreateElement("Sync");
xml.DocumentElement.AppendChild(xmlSync);
xml.CreateAttribute("version");
xml.CreateAttribute("branch");

// Set the attribute values for the sync configuration
xmlSync.SetAttribute("version", SyncVersion);
xmlSync.SetAttribute("branch", SyncBranch);

// Navigates to this node
xml.SelectSingleNode("//Build/Sync");

// Creates a new element for the sync configuration
XmlElement xmlSyncDir = xml.CreateElement("Directories");
xml.DocumentElement.AppendChild(xmlSync);

// Set the attribute values for the sync configuration
xmlSyncDir.SetAttribute("basePath", SyncBasePath);

xml.Save(fileName);
return null;
}

But it's creating like this:

<Build>
<Build type="Daily" sync="True" compile="True" assemble="True" >

<Sync version="1.0.0.0" branch="QA">
<Directories basePath="//OpicsPlus/Source/Release" />
</Sync>
</Build>
</Build>

Anybody knows how can I remove everything and start from scratch ?

Thanks!

Eduardo

--
Eduardo de Morais Ferrari
Misys OPICS Project
Stefanini IT Solutions
White Plains, NY
Phone: (914) 821-2727
Cell: (914) 406-5027
Join Bytes!
6 Answers Posted
Ferrari, Eduardo's Avatar
Ferrari, Eduardo November 12th, 2005 05:08 AM
Guest - n/a Posts
#2: Re: Problem creating XML file...

Hi all

Please read this line:

// Creates a new element for the build configuration
XmlElement xmlBuild = xml.CreateElement("Build");
xml.DocumentElement.AppendChild(xmlBuild);

Instead of

// Creates a new element for the build configuration
XmlElement xmlBuild = xml.CreateElement("//Build");
xml.DocumentElement.AppendChild(xmlBuild);

Thanks!
--
Eduardo de Morais Ferrari
Misys'''''''' OPICS Project
Stefanini IT Solutions
White Plains, NY
Phone: (914) 821-2727
Cell: (914) 406-5027
Join Bytes!


"Ferrari, Eduardo" wrote:
[color=blue]
> Hi all!
>
> I'm trying to create this XML file:
>
> <?xml version="1.0" encoding="utf-8" ?>
> <Build type="Daily" sync="True" compile="True" assemble="True" >
>
> <Sync version="1.0.0.0" branch="QA">
> <Directories basePath="//OpicsPlus/Source/Release" />
> </Sync>
> </Build>
>
> For this, I'm trying to use the code below:
>
> private static BuildConfiguration SaveXML(XmlDocument xml, string fileName)
> {
> // Creates the nodelist
> XmlNodeList nodeList;
> XmlElement user = xml.DocumentElement;
> nodeList = user.SelectNodes("//Build");
>
> // Clear all the node
> user.RemoveAll();
> xml.Save(fileName);
>
> // Creates a new element for the build configuration
> XmlElement xmlBuild = xml.CreateElement("//Build");
> xml.DocumentElement.AppendChild(xmlBuild);
>
> // Navigates to this node
> xml.SelectSingleNode("//Build");
>
> xml.CreateAttribute("type");
> xml.CreateAttribute("sync");
> xml.CreateAttribute("compile");
> xml.CreateAttribute("assemble");
>
> // Set the attribute values for the build configuration
> xmlBuild.SetAttribute("type", BuildType);
> xmlBuild.SetAttribute("sync", BuildSync.ToString());
> xmlBuild.SetAttribute("compile", BuildCompile.ToString());
> xmlBuild.SetAttribute("assemble", BuildAssemble.ToString());
>
> // Navigates to this node
> xml.SelectSingleNode("//Build");
>
> // Creates a new element for the sync configuration
> XmlElement xmlSync = xml.CreateElement("Sync");
> xml.DocumentElement.AppendChild(xmlSync);
> xml.CreateAttribute("version");
> xml.CreateAttribute("branch");
>
> // Set the attribute values for the sync configuration
> xmlSync.SetAttribute("version", SyncVersion);
> xmlSync.SetAttribute("branch", SyncBranch);
>
> // Navigates to this node
> xml.SelectSingleNode("//Build/Sync");
>
> // Creates a new element for the sync configuration
> XmlElement xmlSyncDir = xml.CreateElement("Directories");
> xml.DocumentElement.AppendChild(xmlSync);
>
> // Set the attribute values for the sync configuration
> xmlSyncDir.SetAttribute("basePath", SyncBasePath);
>
> xml.Save(fileName);
> return null;
> }
>
> But it's creating like this:
>
> <Build>
> <Build type="Daily" sync="True" compile="True" assemble="True" >
>
> <Sync version="1.0.0.0" branch="QA">
> <Directories basePath="//OpicsPlus/Source/Release" />
> </Sync>
> </Build>
> </Build>
>
> Anybody knows how can I remove everything and start from scratch ?
>
> Thanks!
>
> Eduardo
>
> --
> Eduardo de Morais Ferrari
> Misys OPICS Project
> Stefanini IT Solutions
> White Plains, NY
> Phone: (914) 821-2727
> Cell: (914) 406-5027
> Join Bytes![/color]
Martin Honnen's Avatar
Guest - n/a Posts
#3: Re: Problem creating XML file...



Ferrari, Eduardo wrote:

[color=blue]
> private static BuildConfiguration SaveXML(XmlDocument xml, string fileName)
> {
> // Creates the nodelist
> XmlNodeList nodeList;
> XmlElement user = xml.DocumentElement;[/color]

If you want to remove the DocumentElement from the document then do
xml.DocumentElement.ParentNode.RemoveChild(xml.Doc umentElement);
Then create a new element e.g.
XmlElement element = xml.CreateElement("Build");
element.SetAttribute("type", "Daily");
// set other attributes the same way
// then append element to document
xml.AppendChild(element);
// then create next element
XmlElement element1 = xml.CreateElement("Sync");
element1.SetAttribute("version", "1.0.0.0");
// set other attributes the same way
// then append element as needed e.g.
element.AppendChild(element1);

With the DOM you need to do two steps, first create a node (Create...),
then insert it into another node (AppendChild (or InsertBefore)).


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Ferrari, Eduardo's Avatar
Ferrari, Eduardo November 12th, 2005 05:08 AM
Guest - n/a Posts
#4: Re: Problem creating XML file...

Hi Thanks!

Almost worked!

It blew up when tryed to add the new attribute:

// Remove the root DocumentElement from the document
xml.DocumentElement.ParentNode.RemoveChild(xml.Doc umentElement);

// Create a new element for the build configuration
XmlElement xmlBuild = xml.CreateElement("Build");
xmlBuild.SetAttribute("type", BuildType);
xmlBuild.SetAttribute("sync", BuildSync.ToString());
xmlBuild.SetAttribute("compile", BuildCompile.ToString());
xmlBuild.SetAttribute("assemble", BuildAssemble.ToString());

// Append element to document
xml.AppendChild(xmlBuild);

// Creates a new element for the sync configuration
XmlElement xmlSync = xml.CreateElement("Sync");

// Set the attribute values for the sync configuration
xmlSync.SetAttribute("version", SyncVersion);
xmlSync.SetAttribute("branch", SyncBranch);

// Append element to document
xml.AppendChild(xmlSync); <--- BLOWS UP HERE

Unhandled Exception: System.InvalidOperationException: This document already
has a DocumentElement node.
at System.Xml.XmlDocument.IsValidChildType(XmlNodeTyp e type)
at System.Xml.XmlNode.AppendChild(XmlNode newChild)


--
Eduardo de Morais Ferrari
Misys'''''''' OPICS Project
Stefanini IT Solutions
White Plains, NY
Phone: (914) 821-2727
Cell: (914) 406-5027
Join Bytes!


"Martin Honnen" wrote:
[color=blue]
>
>
> Ferrari, Eduardo wrote:
>
>[color=green]
> > private static BuildConfiguration SaveXML(XmlDocument xml, string fileName)
> > {
> > // Creates the nodelist
> > XmlNodeList nodeList;
> > XmlElement user = xml.DocumentElement;[/color]
>
> If you want to remove the DocumentElement from the document then do
> xml.DocumentElement.ParentNode.RemoveChild(xml.Doc umentElement);
> Then create a new element e.g.
> XmlElement element = xml.CreateElement("Build");
> element.SetAttribute("type", "Daily");
> // set other attributes the same way
> // then append element to document
> xml.AppendChild(element);
> // then create next element
> XmlElement element1 = xml.CreateElement("Sync");
> element1.SetAttribute("version", "1.0.0.0");
> // set other attributes the same way
> // then append element as needed e.g.
> element.AppendChild(element1);
>
> With the DOM you need to do two steps, first create a node (Create...),
> then insert it into another node (AppendChild (or InsertBefore)).
>
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/
>[/color]
Ferrari, Eduardo's Avatar
Ferrari, Eduardo November 12th, 2005 05:08 AM
Guest - n/a Posts
#5: Re: Problem creating XML file...

Hi

I solved the error using:

// Append element to document
xml.DocumentElement.AppendChild(xmlSync);

But, still is not in the format that I want...

<Sync version="1.0.0.0" branch="QA">
<Directories basePath="OpicsPlus\Source\Release1.0.0.0\QA" all="true" />
</Sync>

It's writing like this:

<Build type="System" sync="True" compile="True" assemble="True">
<Sync version="1.0.0.0" branch="QA" />
<Directories basePath="//OpicsPlus/Source/Release" />
</Build>
--
Eduardo de Morais Ferrari
Misys'''''''' OPICS Project
Stefanini IT Solutions
White Plains, NY
Phone: (914) 821-2727
Cell: (914) 406-5027
Join Bytes!


"Ferrari, Eduardo" wrote:
[color=blue]
> Hi Thanks!
>
> Almost worked!
>
> It blew up when tryed to add the new attribute:
>
> // Remove the root DocumentElement from the document
> xml.DocumentElement.ParentNode.RemoveChild(xml.Doc umentElement);
>
> // Create a new element for the build configuration
> XmlElement xmlBuild = xml.CreateElement("Build");
> xmlBuild.SetAttribute("type", BuildType);
> xmlBuild.SetAttribute("sync", BuildSync.ToString());
> xmlBuild.SetAttribute("compile", BuildCompile.ToString());
> xmlBuild.SetAttribute("assemble", BuildAssemble.ToString());
>
> // Append element to document
> xml.AppendChild(xmlBuild);
>
> // Creates a new element for the sync configuration
> XmlElement xmlSync = xml.CreateElement("Sync");
>
> // Set the attribute values for the sync configuration
> xmlSync.SetAttribute("version", SyncVersion);
> xmlSync.SetAttribute("branch", SyncBranch);
>
> // Append element to document
> xml.AppendChild(xmlSync); <--- BLOWS UP HERE
>
> Unhandled Exception: System.InvalidOperationException: This document already
> has a DocumentElement node.
> at System.Xml.XmlDocument.IsValidChildType(XmlNodeTyp e type)
> at System.Xml.XmlNode.AppendChild(XmlNode newChild)
>
>
> --
> Eduardo de Morais Ferrari
> Misys'''''''' OPICS Project
> Stefanini IT Solutions
> White Plains, NY
> Phone: (914) 821-2727
> Cell: (914) 406-5027
> Join Bytes!
>
>
> "Martin Honnen" wrote:
>[color=green]
> >
> >
> > Ferrari, Eduardo wrote:
> >
> >[color=darkred]
> > > private static BuildConfiguration SaveXML(XmlDocument xml, string fileName)
> > > {
> > > // Creates the nodelist
> > > XmlNodeList nodeList;
> > > XmlElement user = xml.DocumentElement;[/color]
> >
> > If you want to remove the DocumentElement from the document then do
> > xml.DocumentElement.ParentNode.RemoveChild(xml.Doc umentElement);
> > Then create a new element e.g.
> > XmlElement element = xml.CreateElement("Build");
> > element.SetAttribute("type", "Daily");
> > // set other attributes the same way
> > // then append element to document
> > xml.AppendChild(element);
> > // then create next element
> > XmlElement element1 = xml.CreateElement("Sync");
> > element1.SetAttribute("version", "1.0.0.0");
> > // set other attributes the same way
> > // then append element as needed e.g.
> > element.AppendChild(element1);
> >
> > With the DOM you need to do two steps, first create a node (Create...),
> > then insert it into another node (AppendChild (or InsertBefore)).
> >
> >
> > --
> >
> > Martin Honnen --- MVP XML
> > http://JavaScript.FAQTs.com/
> >[/color][/color]
Martin Honnen's Avatar
Guest - n/a Posts
#6: Re: Problem creating XML file...



Ferrari, Eduardo wrote:

[color=blue]
> It blew up when tryed to add the new attribute:[/color]
[color=blue]
> // Append element to document
> xml.AppendChild(xmlSync); <--- BLOWS UP HERE[/color]

That is not what I have shown you, my post had

XmlElement element = xml.CreateElement("Build");
element.SetAttribute("type", "Daily");
// set other attributes the same way
// then append element to document
xml.AppendChild(element);
// then create next element
XmlElement element1 = xml.CreateElement("Sync");
element1.SetAttribute("version", "1.0.0.0");
// set other attributes the same way
// then append element as needed e.g.
element.AppendChild(element1);

you need to call AppendChild on the node into which you want to insert
the node passed as an argument so in your case you want

xmlBuild.AppendChild(xmlSync);


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Ferrari, Eduardo's Avatar
Ferrari, Eduardo November 12th, 2005 05:08 AM
Guest - n/a Posts
#7: Re: Problem creating XML file...

Alright!

Now worked. Sorry I messed up!

Thanks!

Eduardo

--
Eduardo de Morais Ferrari
Misys'''''''' OPICS Project
Stefanini IT Solutions
White Plains, NY
Phone: (914) 821-2727
Cell: (914) 406-5027
Join Bytes!


"Martin Honnen" wrote:
[color=blue]
>
>
> Ferrari, Eduardo wrote:
>
>[color=green]
> > It blew up when tryed to add the new attribute:[/color]
>[color=green]
> > // Append element to document
> > xml.AppendChild(xmlSync); <--- BLOWS UP HERE[/color]
>
> That is not what I have shown you, my post had
>
> XmlElement element = xml.CreateElement("Build");
> element.SetAttribute("type", "Daily");
> // set other attributes the same way
> // then append element to document
> xml.AppendChild(element);
> // then create next element
> XmlElement element1 = xml.CreateElement("Sync");
> element1.SetAttribute("version", "1.0.0.0");
> // set other attributes the same way
> // then append element as needed e.g.
> element.AppendChild(element1);
>
> you need to call AppendChild on the node into which you want to insert
> the node passed as an argument so in your case you want
>
> xmlBuild.AppendChild(xmlSync);
>
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/
>[/color]
 
Not the answer you were looking for? Post your question . . .
196,844 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,844 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors