Solved

Creating a new node

Posted on 2009-03-29
4
287 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
  • 3
4 Comments
 
LVL 3

Expert Comment

by:TommyTwoPints
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP and Soap 3 26
Limiting string to two deciamls 18 30
Hide Tab Page 3 18
Unable  to create new object 9 19
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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now