Solved

Creating a new node

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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

690 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