|
-
January 28th, 2011, 05:52 PM
#1
Help with XML element editing
I have an applications that I'm writing that has an XML file with defined elements. I simply want to take input from textboxes and put the data into the appropriate XML elements.
How would I go about doing this?
Thanks!
-
January 28th, 2011, 08:41 PM
#2
Re: Help with XML element editing
Look at the System.Xml namespace (particulary, XmlTextWriter)
-
January 31st, 2011, 04:12 PM
#3
Re: Help with XML element editing
Here's the code I'm come up with.
XDocument xmlWorldFile = XDocument.Load(@strProjectsPath + "\\" + strFolderName + "\\world_file.xml");
XElement elmWorld = xmlWorldFile.Descendants("world").FirstOrDefault();
if (elmWorld != null)
{
XElement elmWorldName = elmWorld.Element("name");
elmWorldName.SetAttributeValue("VALUE", tbWorldName.Text);
XElement elmWorldGenre = elmWorld.Element("genre");
elmWorldGenre.SetAttributeValue("VALUE", cbWorldGenre.SelectedText);
XElement elmWorldDesc = elmWorld.Element("description");
elmWorldDesc.SetAttributeValue("VALUE", tbWorldDesc.Text);
elmWorldDesc.Save(@strProjectsPath + "\\" + strFolderName + "\\world_file.xml");
}
Here's the XML file being used.
<?xml version='1.0' encoding='UTF-8'?>
<!-- This document was created with Syntext Serna Free. -->
<WORLD>
<NAME><NAME/>
<GENRE><GENRE/>
<DESCRIPTION><DESCRIPTION/>
</WORLD>
Now I'm getting the following error "The 'DESCRIPTION' start tag on line 6 position 4 does not match the end tag of 'WORLD'. Line 7, position 3.
What is causing this error??
-
January 31st, 2011, 10:51 PM
#4
Re: Help with XML element editing
your three inner tags are wrong... they should be
Code:
<name></name>
<genere></genre>
<description></description>
OR
Code:
<name />
<genre />
<description />
but not both.
As a side note... you're creating elements, but not adding them to the world element... they need to be appended. Additionally, you're creating the value as a VALUE ATTRIBUTE... which will make it look like this:
Code:
<genre VALUE="Some value here"/>
I think what you really mean to do is set the VALUE PROPERTY of the ELEMENT
Code:
XElement elmWorldGenre = elmWorld.Element("genre");
elmWorldGenre.value = "Some value here";
Which will give you:
Code:
<genre>Some value here</genre>
-tg
-
February 1st, 2011, 05:21 PM
#5
Re: Help with XML element editing
Thanks for the update. It mostly worked.
Here's what I got:
XDocument xmlWorldFile = XDocument.Load(strProjectsPath + "\\" + strFolderName + "\\world_file.xml");
XElement elmWorld = xmlWorldFile.Descendants("world").FirstOrDefault();
if (elmWorld != null)
{
XElement elmWorldName = elmWorld.Element("name");
elmWorldName.Value = tbWorldName.Text;
XElement elmWorldGenre = elmWorld.Element("genre");
elmWorldGenre.Value = cbWorldGenre.SelectedText;
XElement elmWorldDesc = elmWorld.Element("description");
elmWorldDesc.Value = tbWorldDesc.Text;
elmWorldDesc.Save(strProjectsPath + "\\" + strFolderName + "\\world_file.xml");
}
Here's my new XML file:
<?xml version='1.0' encoding='UTF-8'?>
<!-- This document was created with Syntext Serna Free. -->
<world>
<name></name>
<genre></genre>
<description></description>
</world>
After the code is run, here's what happens to the XML file:
<?xml version="1.0" encoding="utf-8"?>
<description>asdgsag</description>
What did I do wrong?
-
February 1st, 2011, 06:54 PM
#6
Re: Help with XML element editing
As I siad the first time: "you're creating elements, but not adding them to the world element" ... To create an xml document in this manner, you create a root element... child nodes, and append the child nodes to the root node.
-tg
-
February 5th, 2011, 07:59 AM
#7
Re: Help with XML element editing
Hi it's my first post on code guru hope it would be useful.
May I suggest to you my version of solving this problem? Of course this code is ugly but it does what you want.
 Originally Posted by Darkmatter5
Thanks for the update. It mostly worked.
XDocument xmlWorldFile = XDocument.Load(strProjectsPath + "\\" + strFolderName + "\\world_file.xml");
XElement elmWorld = xmlWorldFile.Descendants("world").FirstOrDefault();
if (elmWorld != null)
{
//Loops among all existing elements inside of element "world" (in your case)
foreach(XElement elm in elmWorld.Elements())
{
if (elm.Name == "name")
{
elm.Value = tbWorldName.Text;
}
else if (elm.Name == "genre")
{
elm.Value = cbWorldGenre.SelectedText;
}
else if (elm.Name == "description")
{
elm.Value = tbWorldDesc.Text;
}
}
// In your last code post your save only last element "description", here you
// call method Save() for entire document
xmlWorldFile.Save(strProjectsPath + "\\" + strFolderName + "\\world_file.xml");
}
-
February 5th, 2011, 08:12 AM
#8
Re: Help with XML element editing
But in that case, be aware that if you put in element "world" more than one set of "name-genre-description" last code sample will erase all set to last inputtet.
If you plan to store in xml file more than one set of name-genre-description you probably want to use some sing like this:
<world>
<firstItem>
<name></name>
<genre></genre>
<description></description>
</firstItem>
<secondItem>
<name></name>
<genre></genre>
<description></description>
</secondItem>
</world>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|