473,747 Members | 2,276 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

adding row to exiting xml file problem.

I have the following code that inserts a new element and its attributes into
an xml file. The problem is the syntax of the inserted row. here is my code:

'Create an XmlDataDocument .
Dim doc As XmlDataDocument = New XmlDataDocument
'Load the schema.
doc.DataSet.Rea dXmlSchema("c:\ animal.xsd")
Dim dtAnimals As DataTable = doc.DataSet.Tab les.Item("anima l")
'Load the XML data.
Dim reader As XmlTextReader = New XmlTextReader(" c:\animal.xml")
doc.Load(reader )
Dim root As XmlNode = doc.DocumentEle ment

' Create new animal element
Dim animalElement As XmlElement = doc.CreateEleme nt("animal")
root.AppendChil d(animalElement )

' Set animal attributes (type, name, age)
Dim typeAttribute As XmlAttribute = doc.CreateAttri bute("type")
typeAttribute.I nnerText = "dog"
animalElement.S etAttributeNode (typeAttribute)

Dim nameAttribute As XmlAttribute = doc.CreateAttri bute("name")
nameAttribute.I nnerText = "snoopy"
animalElement.S etAttributeNode (nameAttribute)

Dim ageAttribute As XmlAttribute = doc.CreateAttri bute("age")
ageAttribute.In nerText = "10"
animalElement.S etAttributeNode (ageAttribute)

'Save File
doc.Save("c:\an imal.xml")

The above code gives mr the following xml file, notice the closing animal
tag in the second row:

<?xml version="1.0"?>
<animal type="cat" name="garfield" age="3" />
<animal type="dog" name="snoopy" age="10"></animal>

I need my xml file to look like this:

<?xml version="1.0"?>
<animal type="cat" name="garfield" age="3" />
<animal type="dog" name="snoopy" age="10" />

Any ideas how I can accomplish this? I would perfer to use the
xmlDataDocument object as I will also have comments in the xml file that will
need to stay, and will most likly be displaying the xml file in a datagrid.

I need the
Nov 12 '05 #1
1 1405
"Todd" <Todd @discussions.mi crosoft.com> wrote in message news:0E******** *************** ***********@mic rosoft.com...
doc.Save("c:\an imal.xml")

The above code gives mr the following xml file, notice the closing animal
tag in the second row: \__> <animal type="dog" name="snoopy" age="10"></animal> I need my xml file to look like this:

\__> <animal type="dog" name="snoopy" age="10" />

Of course, these are perfectly equivalent representations (but you probably
already know that and are just handcuffed by a crumby XML consumer).

Instead of letting the Save( ) function wrap the file stream associated with
'animal.xml' in it's own XmlTextWriter, you need to supply your own. This
is because Save( ) will call WriteFullEndEle ment( ). You can't change that
Save( ) is implemented this way, but you can get around it with a custom
XmlTextWriter that performs a little sleight-of-hand by wiring these calls
to WriteEndElement ( ).

An example of an XmlTextWriter that will do this is the following class.
[Vaguely reminiscent of the one I posted yesterday but in VB.NET.] ;-)

- - - XmlTextWriterEE .vb
Imports System
Imports System.IO
Imports System.Xml

' Wrapper that forces more compact empty element end
' tags to be written whenever possible.
Public Class XmlTextWriterEE
Inherits XmlTextWriter

' Create a new instance of XmlTextWriterEE around a TextWriter
' My base class does all the work.
Public Sub New( ByVal sink As System.IO.TextW riter sink )
MyBase.New( sink)
End Sub

' Intercept calls from XML producers that insist on always emiting
' a full end element (whether or not I've written child nodes) and
' route them to WriteEndElement instead.
Public Sub Overrides WriteFullEndEle ment( )
MyBase.WriteEnd Element( )
End Sub

End Class
- - -

This XmlTextWriterEE class works with XmlDocument's Save( ) method in
this way,

Dim writerInner As TextWriter = New StreamWriter( "c:\animal.xml" , False )
Dim writer As New XmlTextWriterEE ( writerInner)
doc.Save( writer)
Derek Harmon
Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Hal Vaughan | last post by:
I have a class that is sometimes called from the command line and sometimes from another class. When the user clicks "Quit" on the panel it opens, if it is running from the command line, it quits. The problem is that I need a way to terminate the class and the object without using quit, so it doesn't kill other classes. The QUIT button calls an actionListener, so if I have "return" in the listener, it closes the window, but the class is...
by: Bonj | last post by:
When trying to debug an ASP.NET project by placing a breakpoint in one of the functions in the codebehind, VS correctly stops at the breakpoint, and I can step through. But soon after, the process stops dead and can't be stepped through anymore (even though there is no error) - I'm guessing that the aspnet_wp.exe is detecting that if it's inactive for a period of n seconds or so, then it exits until it gets another request. Is there...
by: Tom the Canuck | last post by:
A while back I was playing with C++ and made a simple program with a WAV file as a resource. It worked well and was easy to make. I then went on to try this with VB. I had problems. Can this be done in VB? I have VB6 (Visual Studio 6 Enterprise edition).. Any pointers to get me going in the right direction would help. I like to learn on my own. Making a mistake and figuring out where the error was made enhances the learning experience. It...
by: Tim Werth | last post by:
I have a .NET console application that is kicked off by a .NET Windows service. They communicate via .NET Remoting, although there isn't much communication between the two while the console app is running. There can be, and are typically, several instances of the console app running at the same time communicating back to the service. The console app creates a delegate to do some work while the main thread waits on a ManualResetEvent to...
by: Kannan | last post by:
Hi, I am trying to created Outloook Add-in Com in outlook using C#. I have seen this URL for developing this sample http://support.microsoft.com/?kbid=302901 When I executed this program it created new custom button called "My Custom Button" in the Outlook menu bar. But when I tried to create one more button called "Forward Mail" (code is
by: Ant | last post by:
Hi all, I'm putting together a simple help module for my applications, using html files stored in the application directory somewhere. Basically it sets up a basic web server, and then uses the webbrowser module to display it in the users browser. I have it set up to call sys.exit(0) if the url quit.html is called, but for some reason (on Windows XP via the cmd.exe shell) python won't let me have the console back. I get the System Exit...
by: John (Z R) L | last post by:
Hi all, I am very new to programming, and I chose to study the Python language before C++. I am currently using the Wikibooks "Non-Programmer's Tutorial for Python", and am up to the section "Who goes there"? http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python/Who_Goes_There%3F But after clicking "run module" for " a = 1
by: Neo | last post by:
I want to log information for a function call like void Add(void) { } When above function gets called, it should output strings like Entering Add Exiting Add
by: care02 | last post by:
Hi! I have the following problem: I have written a short Python server that creates an indefinite simulation thread that I want to kill when quitting (Ctrl-C) from Python. Googling around has not given me any hints on how to cleanly kill running threads before exiting. Any help is appreciated! Carl
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.