Solved

Creating a new node

Posted on 2009-03-29
4
291 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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