Groups | Blog | Home
all groups > dotnet xml > october 2005 >

dotnet xml : Problem creating XML file...


Martin Honnen
10/5/2005 12:00:00 AM


[quoted text, click to view]


[quoted text, click to view]

If you want to remove the DocumentElement from the document then do
xml.DocumentElement.ParentNode.RemoveChild(xml.DocumentElement);
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
Martin Honnen
10/5/2005 12:00:00 AM


[quoted text, click to view]


[quoted text, click to view]

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
Ferrari, Eduardo
10/5/2005 7:25:23 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
Ferrari, Eduardo
10/5/2005 7:34:02 AM
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
eduardo.ferrari@misys.com


[quoted text, click to view]
Ferrari, Eduardo
10/5/2005 8:48:07 AM
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.DocumentElement);

// 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(XmlNodeType 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
eduardo.ferrari@misys.com


[quoted text, click to view]
Ferrari, Eduardo
10/5/2005 10:09:01 AM
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
eduardo.ferrari@misys.com


[quoted text, click to view]
Ferrari, Eduardo
10/5/2005 10:28:03 AM
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
eduardo.ferrari@misys.com


[quoted text, click to view]
AddThis Social Bookmark Button