Append Child Nodes XML in Memory

G

Guest

I know I have been asking LOTS of xml questions and I really apprecaite all
the help.

Here is my XML file
<?xml version="1.0" encoding="utf-8" ?>

<products>
<!-- Repeat the structure below for each product. -->
<product>
<id>CP-90, T-32</id>
<company>Carwell</company>
<address>Street Address</address>
<city>City</city>
<state>State</state>
<zip>Zip</zip>
</product>

<!-- Next product here-->
</products>


I am loading the doc in memory using xmlDocument
Dim xmlFile As String = xmlLocation()
Dim xmlDoc As XmlDocument

Private Sub Products_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
xmlDoc = New XmlDocument
xmlDoc.Load(xmlFile)


End Sub

Here is when my problem starts. I have a form with all the information that
I needed "appended" to the XMLdoc (in memory). I tried

Private Sub AddProduct(ByVal node As XmlNode)
Dim xmlProductName = txtProduct.Text
Dim xmlCompanyName = txtCompany.Text
Dim xmlAddress = txtAddress.Text
Dim xmlCity = txtCity.Text
Dim xmlState = cmboState.Text
Dim xmlZip = txtZip.Text
node.AppendChild(xmlProductName)
node.AppendChild(xmlCompanyName)
node.AppendChild(xmlAddress)
node.AppendChild(xmlCity)
node.AppendChild(xmlState)
node.AppendChild(xmlZip)
End Sub

But I am really confused on what node to pass. Any help would be apprciated.
 
S

Sean Hederman

Whoops! That was silly, rather do:
AddProduct(xmlDoc.ChildNodes(0))
It'll be faster than using XPath.
 
G

Guest

Now I am getting this error

Additional information: Specified cast is not valid.

error begins on this line
node.AppendChild(xmlProductName)

Thanks for the help.

Michae
 
G

Guest

Here is my debug output.

'DefaultDomain': Loaded
'c:\winnt\microsoft.net\framework\v1.1.4322\mscorlib.dll', No symbols loaded.
'Zadig': Loaded 'C:\Documents and Settings\mlubrano\My Documents\Visual
Studio Projects\Zadig\bin\Zadig.exe', Symbols loaded.
'Zadig.exe': Loaded
'c:\winnt\assembly\gac\system.windows.forms\1.0.5000.0__b77a5c561934e089\system.windows.forms.dll', No symbols loaded.
'Zadig.exe': Loaded
'c:\winnt\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll', No
symbols loaded.
'Zadig.exe': Loaded
'c:\winnt\assembly\gac\microsoft.visualbasic\7.0.5000.0__b03f5f7f11d50a3a\microsoft.visualbasic.dll', No symbols loaded.
'Zadig.exe': Loaded
'c:\winnt\assembly\gac\system.drawing\1.0.5000.0__b03f5f7f11d50a3a\system.drawing.dll', No symbols loaded.
'Zadig.exe': Loaded
'c:\winnt\assembly\gac\system.xml\1.0.5000.0__b77a5c561934e089\system.xml.dll', No symbols loaded.
An unhandled exception of type 'System.InvalidCastException' occurred in
Zadig.exe

Additional information: Specified cast is not valid.


Unhandled Exception: System.InvalidCastException: Specified cast is not valid.
at Zadig.Products.AddProduct(XmlNode node) in C:\Documents and
Settings\mlubrano\My Documents\Visual Studio Projects\Zadig\Products.vb:line
239
at Zadig.Products.bttnAdd_Click(Object sender, EventArgs e) in
C:\Documents and Settings\mlubrano\My Documents\Visual Studio
Projects\Zadig\Products.vb:line 223
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Zadig.Zadig.Main() in C:\Documents and Settings\mlubrano\My
Documents\Visual Studio Projects\Zadig\Main.vb:line 3The program '[328]
Zadig.exe' has exited with code 0 (0x0).
 
S

Sean Hederman

That would generally be caused by you not passing an XmlNode into the
method. Please can you put a breakpoint onto AddProduct(XmlNode node), and
let us know what runtime type is reported by VS.NET for the parameter being
passed in.

fremenusul said:
Here is my debug output.

'DefaultDomain': Loaded
'c:\winnt\microsoft.net\framework\v1.1.4322\mscorlib.dll', No symbols
loaded.
'Zadig': Loaded 'C:\Documents and Settings\mlubrano\My Documents\Visual
Studio Projects\Zadig\bin\Zadig.exe', Symbols loaded.
'Zadig.exe': Loaded
'c:\winnt\assembly\gac\system.windows.forms\1.0.5000.0__b77a5c561934e089\system.windows.forms.dll',
No symbols loaded.
'Zadig.exe': Loaded
'c:\winnt\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll', No
symbols loaded.
'Zadig.exe': Loaded
'c:\winnt\assembly\gac\microsoft.visualbasic\7.0.5000.0__b03f5f7f11d50a3a\microsoft.visualbasic.dll',
No symbols loaded.
'Zadig.exe': Loaded
'c:\winnt\assembly\gac\system.drawing\1.0.5000.0__b03f5f7f11d50a3a\system.drawing.dll',
No symbols loaded.
'Zadig.exe': Loaded
'c:\winnt\assembly\gac\system.xml\1.0.5000.0__b77a5c561934e089\system.xml.dll',
No symbols loaded.
An unhandled exception of type 'System.InvalidCastException' occurred in
Zadig.exe

Additional information: Specified cast is not valid.


Unhandled Exception: System.InvalidCastException: Specified cast is not
valid.
at Zadig.Products.AddProduct(XmlNode node) in C:\Documents and
Settings\mlubrano\My Documents\Visual Studio
Projects\Zadig\Products.vb:line
239
at Zadig.Products.bttnAdd_Click(Object sender, EventArgs e) in
C:\Documents and Settings\mlubrano\My Documents\Visual Studio
Projects\Zadig\Products.vb:line 223
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Zadig.Zadig.Main() in C:\Documents and Settings\mlubrano\My
Documents\Visual Studio Projects\Zadig\Main.vb:line 3The program '[328]
Zadig.exe' has exited with code 0 (0x0).



fremenusul said:
Now I am getting this error

Additional information: Specified cast is not valid.

error begins on this line
node.AppendChild(xmlProductName)

Thanks for the help.

Michae
 
G

Guest

Got the answer
(http://msdn.microsoft.com/newsgroup...eral&mid=66e63b29-4825-4519-b7b9-5639e23d5cb4)

I do have another question about reading xml from memory though....
I am building a application that uses XML data to display prducts.

Currently, I have a class that holds the XML data.

Imports System.Xml

Public Class Settings

Private Shared xmlDoc As XmlDocument

Public Shared Property Products() As XmlDocument
Get
If xmlDoc Is Nothing Then
Dim xmlFile As String = xmlLocation()
Dim xmlDoc As XmlDocument

xmlDoc = New XmlDocument
xmlDoc.Load(xmlFile)
End If

Return xmlDoc
End Get
Set(ByVal Value As XmlDocument)

End Set
End Property

End Class

Here is where my problem begins. I have a form that has a combobox that will
display some of the xml data. If someone needs to add data a new form pops up
and they add data to the memory (xml). Then it saves the new XML file.

If they close the "add products" form, the combobox is NOT updated with the
new information. How can I have the combobox read the XML data (in memory)?

PS: here is how I was doing it before I added a class to hold XML data.
Private Sub Zadig_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Dim xmlTr As New XmlTextReader(xmlFile)

'While xmlTr.Read
' If xmlTr.Name = "id" AndAlso xmlTr.NodeType =
XmlNodeType.Element Then
' cmbProducts.Items.Add(xmlTr.ReadString)
' End If
'End While
'xmlTr.Close()

'xmlDoc = New XmlDocument
'xmlDoc.Load(xmlFile)

End Sub

I dont think I can do it in the "load" area because it is only loaded once.
I need something that will be "dynamic" with the combobox.

Sean Hederman said:
That would generally be caused by you not passing an XmlNode into the
method. Please can you put a breakpoint onto AddProduct(XmlNode node), and
let us know what runtime type is reported by VS.NET for the parameter being
passed in.

fremenusul said:
Here is my debug output.

'DefaultDomain': Loaded
'c:\winnt\microsoft.net\framework\v1.1.4322\mscorlib.dll', No symbols
loaded.
'Zadig': Loaded 'C:\Documents and Settings\mlubrano\My Documents\Visual
Studio Projects\Zadig\bin\Zadig.exe', Symbols loaded.
'Zadig.exe': Loaded
'c:\winnt\assembly\gac\system.windows.forms\1.0.5000.0__b77a5c561934e089\system.windows.forms.dll',
No symbols loaded.
'Zadig.exe': Loaded
'c:\winnt\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll', No
symbols loaded.
'Zadig.exe': Loaded
'c:\winnt\assembly\gac\microsoft.visualbasic\7.0.5000.0__b03f5f7f11d50a3a\microsoft.visualbasic.dll',
No symbols loaded.
'Zadig.exe': Loaded
'c:\winnt\assembly\gac\system.drawing\1.0.5000.0__b03f5f7f11d50a3a\system.drawing.dll',
No symbols loaded.
'Zadig.exe': Loaded
'c:\winnt\assembly\gac\system.xml\1.0.5000.0__b77a5c561934e089\system.xml.dll',
No symbols loaded.
An unhandled exception of type 'System.InvalidCastException' occurred in
Zadig.exe

Additional information: Specified cast is not valid.


Unhandled Exception: System.InvalidCastException: Specified cast is not
valid.
at Zadig.Products.AddProduct(XmlNode node) in C:\Documents and
Settings\mlubrano\My Documents\Visual Studio
Projects\Zadig\Products.vb:line
239
at Zadig.Products.bttnAdd_Click(Object sender, EventArgs e) in
C:\Documents and Settings\mlubrano\My Documents\Visual Studio
Projects\Zadig\Products.vb:line 223
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Zadig.Zadig.Main() in C:\Documents and Settings\mlubrano\My
Documents\Visual Studio Projects\Zadig\Main.vb:line 3The program '[328]
Zadig.exe' has exited with code 0 (0x0).



fremenusul said:
Now I am getting this error

Additional information: Specified cast is not valid.

error begins on this line
node.AppendChild(xmlProductName)

Thanks for the help.

Michae
:

Whoops! That was silly, rather do:
AddProduct(xmlDoc.ChildNodes(0))
It'll be faster than using XPath.

I know I have been asking LOTS of xml questions and I really
apprecaite all
the help.

Here is my XML file
<?xml version="1.0" encoding="utf-8" ?>

<products>
<!-- Repeat the structure below for each product. -->
<product>
<id>CP-90, T-32</id>
<company>Carwell</company>
<address>Street Address</address>
<city>City</city>
<state>State</state>
<zip>Zip</zip>
</product>

<!-- Next product here-->
</products>


I am loading the doc in memory using xmlDocument
Dim xmlFile As String = xmlLocation()
Dim xmlDoc As XmlDocument

Private Sub Products_Load(ByVal sender As System.Object, ByVal e
As
System.EventArgs) Handles MyBase.Load
xmlDoc = New XmlDocument
xmlDoc.Load(xmlFile)


End Sub

Here is when my problem starts. I have a form with all the
information
that
I needed "appended" to the XMLdoc (in memory). I tried

Private Sub AddProduct(ByVal node As XmlNode)
Dim xmlProductName = txtProduct.Text
Dim xmlCompanyName = txtCompany.Text
Dim xmlAddress = txtAddress.Text
Dim xmlCity = txtCity.Text
Dim xmlState = cmboState.Text
Dim xmlZip = txtZip.Text
node.AppendChild(xmlProductName)
node.AppendChild(xmlCompanyName)
node.AppendChild(xmlAddress)
node.AppendChild(xmlCity)
node.AppendChild(xmlState)
node.AppendChild(xmlZip)
End Sub

But I am really confused on what node to pass. Any help would be
apprciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top