Solved

Creating a new node

Posted on 2009-03-29
4
290 Views
Last Modified: 2012-08-13
I am confused. I am adding a node into a XML file by using this code:
        xmlDoc.Load(A.XMLFile)
        HeaderNode = xmlDoc.SelectSingleNode("//Category)
        Dim ElementToAdd As XmlElement
        ElementToAdd = xmlDoc.CreateElement("Category")
        ElementToAdd.SetAttribute("Name", "CatTest")
        HeaderNode.AppendChild(ElementToAdd)
        xmlDoc.Save(A.XMLFile)

This creates this line in the file
    <Category Name="CatTest" />
As you see there is no closing tab

If I add the line ElementToAdd.InnerText = "innerText" between
        ElementToAdd.SetAttribute("Name", "CatTest")
        HeaderNode.AppendChild(ElementToAdd)

Then this is created and it looks OK
<Category Name="CatTest">innerText</Category>

The poblem is that I don't want any innertext. I want to add the node
<Category Name="CatTest" /Category>
   
Somebody out there who can help me?
0
Comment
Question by:Leif753
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 3

Expert Comment

by:TommyTwoPints
ID: 24014145
Hello Leif753,

 doesnt contain any inner text so it will automatically be closed using the "/>"

 in invalid syntax and would corrupt your XML document.

If you require your XML to look like this:  then you could try setting the inner text of the element to "" or " "
 
Regards,

TommyTwoPints
0
 
LVL 3

Expert Comment

by:TommyTwoPints
ID: 24014261
Hi Leif753,
After doing many tests using an XMLTextWriter and the XMLDocument classes, I cannot get your node displayed in the format you require.
Can I ask why you need the node to be displayed in this format so we can reach a solution.
Regards
TommyTwoPints
0
 

Author Comment

by:Leif753
ID: 24014595
So you mean that a node must have an innerText? I thought the below is correct syntax: (and also I thought this is what was going to be created). The answer to your question is: I don't need to have this format, I am flexible, I can have an empty inner Text

<Category Name="CatTest">
 /Category>

0
 
LVL 3

Accepted Solution

by:
TommyTwoPints earned 125 total points
ID: 24014614
Leif753,

tags are defined by the "<>" parenthesis.

Open tags are always defined as

Close tags are always defined as

Tags with no inner text or inner XML are defined as

The XML code:


 /Category>

Would give an error saying something like: '<' expected at line 2 character 1

TommyTwoPints
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question