473,396 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

XML Parents

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.

Mar 7 '06 #1
5 1456
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);


ma*************@gmail.com wrote:
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.

Mar 7 '06 #2
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?

Mar 10 '06 #3
ma*************@gmail.com wrote:
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

Mar 10 '06 #4
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?

Mar 13 '06 #5
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.

Mar 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dan Williams | last post by:
Hi people I've just joined the Python list and although I'm sure my question must be asked fairly frequently, I have been unable to find an answer after two days of searching. For the record, my...
1
by: John Smith | last post by:
I have a user assigned multiple roles and a role can be inherited from multiple parents (see below). How do I answer such questions as "How many roles does the user belongs to?" I answered the...
3
by: PYCTAM | last post by:
Hi, I have a table with filled out below data: +------+-----+ |parent|child| +------+-----+ |A |B | |B |C | |B |E |
0
by: Hananiel | last post by:
TABLE1 has ID Data Table2 has ID Data Table3 has
1
by: Lucas Graf | last post by:
This sounds so easy to me and feel like an idiot that I can't figure it out, but....how can I have a MDI child access it's parents control(s)?? Seems like it should be as easy as making the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.