Solved

Creating a new node

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
A short film showing how OnPage and Connectwise integration works.

914 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

16 Experts available now in Live!

Get 1:1 Help Now