CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2011
    Posts
    4

    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!

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Help with XML element editing

    Look at the System.Xml namespace (particulary, XmlTextWriter)

  3. #3
    Join Date
    Jan 2011
    Posts
    4

    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??

  4. #4
    Join Date
    Dec 2007
    Posts
    234

    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
    * I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help - how to remove eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
    * On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
    MVP '06-'10

  5. #5
    Join Date
    Jan 2011
    Posts
    4

    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?

  6. #6
    Join Date
    Dec 2007
    Posts
    234

    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
    * I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help - how to remove eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
    * On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
    MVP '06-'10

  7. #7
    Join Date
    Feb 2011
    Posts
    1

    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.

    Quote Originally Posted by Darkmatter5 View Post
    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");
    }

  8. #8
    Join Date
    Feb 2011
    Posts
    2

    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
  •  





Click Here to Expand Forum to Full Width

Featured