XML Parents

  • Thread starter mauricecosgrave
  • Start date
M

mauricecosgrave

Hi,

I'm trying to create an XML file from a C# application. I can create
children of the root using this code:
-------------------------------------------------------------
//Save the variables
pbName = selPBTextbox.Text;
pbUnit = unitList.Text;

XmlDocument xmldoc;
XmlNode xmlnode;
XmlElement global; // Global
XmlElement play; // Title of the playbook
XmlText xmltext;

xmldoc = new XmlDocument();

xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
xmldoc.AppendChild(xmlnode);

global = xmldoc.CreateElement("", "playbook", "");
xmldoc.AppendChild(global);

play= xmldoc.CreateElement("", "play", "");
xmltext = xmldoc.CreateTextNode(pbName);
play.AppendChild(xmltext);
xmldoc.ChildNodes.Item(1).AppendChild(play);
-------------------------------------------------------------

The problem I'm having is in creating a child of a child, for example
if I wanted to add 'title' and 'unit' to 'play':

<playbook>
<play>
<title></title>
<unit></unit>
</play>
<play>
<title></title>
<unit></unit>
</play>
<play>
<title></title>
<unit></unit>
</play>
</playbook>

how would I go about it? Using the code above won't add the 'title'
and 'unit' elements to the 'play' elements, it will just add them at
the same level as 'play'? As you can see I also need to have multiple
occurances of the 'play' element.
 
J

John Murray

Just modifying your code...not trying to make it look pretty. Just keep
a reference to the play node when you are creating it and append the
title and unit children to it.

That being said .... if all you are doing is creating the xml document
in a serial fashion, I highly suggest that you look at the XmlTextWriter
class -- this will make your live a lot easier

John



play = xmldoc.CreateElement("play");
global.AppendChild(play);

XmlNode title = xmldoc.CreateElement("title");
play.AppendChild(title);

XmlNode unit = xmldoc.CreateElement("unit");
play.AppendChild(unit);
 
M

mauricecosgrave

Thanks for your help - apologies for not replying sooner. There just
seems to be a problem with the global.AppendChild(play) line; I'm
getting an error saying "Use of unassigned local variable 'global'".
I've initially declared the 'global' element as an XmlElement - would I
need to declare it as something else seeing as how 'global' is the root
element?
 
J

Jon Skeet [C# MVP]

Thanks for your help - apologies for not replying sooner. There just
seems to be a problem with the global.AppendChild(play) line; I'm
getting an error saying "Use of unassigned local variable 'global'".
I've initially declared the 'global' element as an XmlElement - would I
need to declare it as something else seeing as how 'global' is the root
element?

No - you just need to actually *tell* it that it's the root element.
Given the compiler error, you're never assigning to it. (Or at least,
you're not *definitely* assigning to it.)

Jon
 
M

mauricecosgrave

Thanks for your help so far. I think I may know why I'm having the
problem - I'm declaring the global (root) variable in a previous form
as I need to insert data into the XML file earlier in the application.
This is causing problems as when I tell it that it is the root, I get
an error saying something like this DocumentElement node already exists
- which I think is from the previous form.

What I think I need to do is reference the global element in the XML
document from my current form. If I did that, that reference would
already be set as the root and I could continue as you laid out
previously. Would this be correct? If so, what would be the best way
to solve the problem?
 
M

mauricecosgrave

Ok I figured it out. I was able to carry the variables from the first
form to the second form (because we needed them there anyway) then
based on an event on the second form write everything to the XML file
using the code you (Jon) posted above. It's working grand now. Thanks
again.
 

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

Similar Threads


Top