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!
Printable View
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!
Look at the System.Xml namespace (particulary, XmlTextWriter)
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??
your three inner tags are wrong... they should be
ORCode:<name></name>
<genere></genre>
<description></description>
but not both.Code:<name />
<genre />
<description />
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:
I think what you really mean to do is set the VALUE PROPERTY of the ELEMENTCode:<genre VALUE="Some value here"/>
Which will give you:Code:XElement elmWorldGenre = elmWorld.Element("genre");
elmWorldGenre.value = "Some value here";
-tgCode:<genre>Some value here</genre>
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?
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
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>