I have error in this code:
Dim Doc As XmlDocument = New XmlDocument
Dim Attrb As XmlAttribute
Try
Doc.Load(Application.StartupPath & "\EmployeeDB.xml")
Catch ex As Exception
Dim Dec As XmlDeclaration = Doc.CreateXmlDeclaration("1.0", "utf-8", Nothing) : Doc.AppendChild(Dec)
End Try
Dim Root As XmlElement = Doc.CreateElement("Section")
Attrb = Doc.CreateAttribute( "Index") : Attrb.InnerText = ComboBox1.SelectedIndex
Root.Attributes.Append(Attrb) : Doc.AppendChild(Root)
Dim Child As XmlNode = Doc.CreateElement("Employee")
Attrb = Doc.CreateAttribute( "Name") : Attrb.InnerText = TextBox1.Text
Child.Attributes.Append(Attrb) : Root.AppendChild(Child)
Dim SubChild As XmlNode
SubChild = Doc.CreateElement( "ID") : SubChild.InnerText = TextBox2.Text
Child.AppendChild(SubChild)
SubChild = Doc.CreateElement( "Number") : SubChild.InnerText = TextBox3.Text
Child.AppendChild(SubChild)
SubChild = Doc.CreateElement( "Jop") : SubChild.InnerText = TextBox4.Text
Child.AppendChild(SubChild)
Doc.Save(Application.StartupPath & "\EmployeeDB.xml")
In first time it's work fine but when I try to add another data I get this error "This document already has a 'DocumentElement' node." in Doc.AppendChild(Root) |
| haihtomy Wednesday, August 13, 2008 10:21 AM |
I fix the error and its work fine but your are right it must have one root elemnet so I rebuild my code and add Root name "Sections" then its look like this:
Dim Doc As XmlDocument = New XmlDocument 'Set Xml Document Dim Root As XmlElement = Doc.CreateElement("Sections") 'Set Root Elemnet Dim MainChild As XmlElement = Doc.CreateElement("Section") : MainChild.SetAttribute("index", ComboBox1.SelectedIndex) 'Set Main Child Elemnet Dim Child As XmlElement = Doc.CreateElement("Employee") : Child.SetAttribute("Name", TextBox1.Text) 'Set Child Elemnet Dim SubChild As XmlElement 'Set Sub Child Elemnet Try Doc.Load(Application.StartupPath & "\EmployeeDB.xml") 'Open XML File Catch ex As Exception Dim Dec As XmlDeclaration = Doc.CreateXmlDeclaration("1.0", "utf-8", Nothing) : Doc.AppendChild(Dec) 'Create New XML File End Try SubChild = Doc.CreateElement("ID") Child.AppendChild(SubChild) : SubChild.InnerText = TextBox2.Text SubChild = Doc.CreateElement("Number") Child.AppendChild(SubChild) : SubChild.InnerText = TextBox3.Text SubChild = Doc.CreateElement("Jop") Child.AppendChild(SubChild) : SubChild.InnerText = TextBox4.Text MainChild.AppendChild(Child) Try Root.AppendChild(MainChild) : Doc.AppendChild(Root) 'Add Root & Child Elemnet Catch ex As Exception Doc.SelectNodes("Sections").Item(0).AppendChild(MainChild) 'If Root exist add Child Elemnet only End Try
the resault is:
<?xml version="1.0" encoding="utf-8"?> <Sections> <Section index="2"> <Employee Name="micheal"> <ID>1</ID> <Number>1</Number> <Jop>Nothing</Jop> </Employee> </Section> <Section index="2"> <Employee Name="peter"> <ID>2</ID> <Number>2</Number> <Jop>Nothing</Jop> </Employee> </Section> <Section index="1"> <Employee Name="john"> <ID>3</ID> <Number>3</Number> <Jop>Nothing</Jop> </Employee> </Section> <Section index="1"> <Employee Name="lee"> <ID>4</ID> <Number>4</Number> <Jop>Nothing</Jop> </Employee> </Section> </Sections>
|
| haihtomy Thursday, August 14, 2008 12:43 PM |
The error is thrown because you are trying to add a start element (Document element) to a xmlDoc which already has a Document Element.
Try this :
Doc.DocumentElement.AppendChild(Root)
Hope this helps...
Regards...
Girija Shankar |
| Girija Shankar Wednesday, August 13, 2008 12:11 PM |
It's work but it give me wrong resault:
<?xml version="1.0" encoding="utf-8"?> <Section Index="2"> <Employee Name="Micheal"> <ID>1</ID> <Number>1</Number> <Jop>Nothing</Jop> </Employee> <Section Index="2"> <Employee Name="Tom"> <ID>2</ID> <Number>2</Number> <Jop>Nothing</Jop> </Employee> </Section> </Section>
it must be like that:
<?xml version="1.0" encoding="utf-8"?> <Section Index="2"> <Employee Name="Micheal"> <ID>1</ID> <Number>1</Number> <Jop>Nothing</Jop> </Employee> <Employee Name="Tom"> <ID>2</ID> <Number>2</Number> <Jop>Nothing</Jop> </Employee> </Section>
|
| haihtomy Wednesday, August 13, 2008 1:23 PM |
Try this :
In place of :Root.AppendChild(Child)
in this statement : Child.Attributes.Append(Attrb) : Root.AppendChild(Child)
use:
(I am writing in C#)
if (Root.ChildNodes > 0)
{
Root.FirstChild.AppendChild(Child)
}
else
Root.ChildNodes[0] = Child;
Hope this helps ..
Let me know if this works
Regards..
Girija Shankar |
| Girija Shankar Wednesday, August 13, 2008 2:00 PM |
I can't convert your code online in some web site but mybe there is error in your code.
|
| haihtomy Wednesday, August 13, 2008 8:35 PM |
Sorry Wrong Code :
if (Root.ChildNodes.Count > 0)
{
Root.FirstChild.AppendChild(Child)
}
else
Root.ChildNodes[0] = Child;
Try this. Let me know if this works...
Regards..
Girija Shankar
|
| Girija Shankar Wednesday, August 13, 2008 8:38 PM |
I convert the code like this:
If Root.ChildNodes.Count > 0 Then Root.FirstChild.AppendChild(Child) Else Root.ChildNodes.Item(0) = Child End If
but I get error in "Root.ChildNodes.Item(0)" it say "Expression is a value and therefore cannot be the target of an assignment."
|
| haihtomy Wednesday, August 13, 2008 9:51 PM |
Try this:
If Root.ChildNodes.Count > 0 Then Root.FirstChild.AppendChild(Child) Else Root.ChildNodes.Item(0).AppendChild(Child) End If
Regards..
Girija Shankar
|
| Girija Shankar Wednesday, August 13, 2008 9:57 PM |
Dim Doc As XmlDocument = New XmlDocument Dim Root As XmlElement = Doc.CreateElement("Section") : Root.SetAttribute("index", ComboBox1.SelectedIndex) Dim Child As XmlElement = Doc.CreateElement("Employee") : Child.SetAttribute("Name", TextBox1.Text) Dim SubChild1 As XmlElement = Doc.CreateElement("ID") Dim SubChild2 As XmlElement = Doc.CreateElement("Number") Dim SubChild3 As XmlElement = Doc.CreateElement("Jop")
Try Doc.Load(Application.StartupPath & "\EmployeeDB.xml") Catch ex As Exception Dim Dec As XmlDeclaration = Doc.CreateXmlDeclaration("1.0", "utf-8", Nothing) : Doc.AppendChild(Dec) End Try
Try Doc.AppendChild(Root) Catch ex As Exception Doc.DocumentElement.AppendChild(Root) End Try
If Root.ChildNodes.Count > 0 Then Root.FirstChild.AppendChild(Child) Else Root.ChildNodes.Item(0).AppendChild(Child) End If
Child.AppendChild(SubChild1) : SubChild1.InnerText = TextBox2.Text Child.AppendChild(SubChild2) : SubChild2.InnerText = TextBox3.Text Child.AppendChild(SubChild3) : SubChild3.InnerText = TextBox4.Text
Doc.Save(Application.StartupPath & "\EmployeeDB.xml")
error "Object reference not set to an instance of an object." in "Root.ChildNodes.Item(0).AppendChild(Child)"
|
| haihtomy Wednesday, August 13, 2008 10:31 PM |
Try this code .. This will work .. The one marked in Red is to be removed and marked in yellow to be added:
Dim Doc As XmlDocument = New XmlDocument Dim Root As XmlElement = Doc.CreateElement("Section") : Root.SetAttribute("index", ComboBox1.SelectedIndex) Dim Child As XmlElement = Doc.CreateElement("Employee") : Child.SetAttribute("Name", TextBox1.Text) Dim SubChild1 As XmlElement = Doc.CreateElement("ID") Dim SubChild2 As XmlElement = Doc.CreateElement("Number") Dim SubChild3 As XmlElement = Doc.CreateElement("Jop")
Try Doc.Load(Application.StartupPath & "\EmployeeDB.xml") Catch ex As Exception Dim Dec As XmlDeclaration = Doc.CreateXmlDeclaration("1.0", "utf-8", Nothing) : Doc.AppendChild(Dec) End Try
Try Doc.AppendChild(Root) Catch ex As Exception Doc.DocumentElement.AppendChild(Root) End Try
If Root.ChildNodes.Count > 0 Then Root.FirstChild.AppendChild(Child) Else Root.ChildNodes.Item(0).AppendChild(Child) End If
Child.AppendChild(SubChild1) : SubChild1.InnerText = TextBox2.Text Child.AppendChild(SubChild2) : SubChild2.InnerText = TextBox3.Text Child.AppendChild(SubChild3) : SubChild3.InnerText = TextBox4.Text
Doc.DocumentElement.LastChild.AppendChild(Child)
Doc.Save(Application.StartupPath & "\EmployeeDB.xml")
|
| Girija Shankar Wednesday, August 13, 2008 11:13 PM |
| haihtomy wrote: |
|
I have error in this code:
Dim Doc As XmlDocument = New XmlDocument
Dim Attrb As XmlAttribute
Try
Doc.Load(Application.StartupPath & "\EmployeeDB.xml")
Catch ex As Exception
Dim Dec As XmlDeclaration = Doc.CreateXmlDeclaration("1.0", "utf-8", Nothing) : Doc.AppendChild(Dec)
End Try
Dim Root As XmlElement = Doc.CreateElement("Section")
Attrb = Doc.CreateAttribute("Index") : Attrb.InnerText = ComboBox1.SelectedIndex
Root.Attributes.Append(Attrb) : Doc.AppendChild(Root)
Dim Child As XmlNode = Doc.CreateElement("Employee")
Attrb = Doc.CreateAttribute("Name") : Attrb.InnerText = TextBox1.Text
Child.Attributes.Append(Attrb) : Root.AppendChild(Child)
Dim SubChild As XmlNode
SubChild = Doc.CreateElement("ID") : SubChild.InnerText = TextBox2.Text
Child.AppendChild(SubChild)
SubChild = Doc.CreateElement("Number") : SubChild.InnerText = TextBox3.Text
Child.AppendChild(SubChild)
SubChild = Doc.CreateElement("Jop") : SubChild.InnerText = TextBox4.Text
Child.AppendChild(SubChild)
Doc.Save(Application.StartupPath & "\EmployeeDB.xml")
In first time it's work fine but when I try to add another data I get this error "This document already has a 'DocumentElement' node." in Doc.AppendChild(Root) | | I have highlighted in red in the quote the actual source of the issue. That line is creating the "Section" element, and adding it as the document element. Then, on the yellow line, you are trying to add the same element again as the root (also known as Document Element), so you get the error: "This document already has a 'DocumentElement' node. This happens because indeed, there is already a document element node in the document. In theory, since the element is already added, you shouldn't add it again, so the yellow statement should simply be removed from the code. If that doesn't work, try replacing the line with the red highlight with Dim Root As New XmlElement("Section") this will create the "Section" element, independently of the document, and assign the XmlElement object to the Root variable, so you can work with it. Then your code makes whatever it needs with it (like adding attributes, as you're doing), and only when you finally reach the Doc.AppendChild() call the element is added. Hope this helps. |
| herenvardo Wednesday, August 13, 2008 11:21 PM |
The reason why you are getting the above error is because you cannot have more than one Document Element.
Let me explain :
You can have a XML like this :
<?xml version="1.0" encoding="utf-8"?>
<RootElement> <Section Index="2"> <Employee Name="Micheal"> <ID>1</ID> <Number>1</Number> <Jop>Nothing</Jop> </Employee> </Section>
<Section Index="3"> <Employee Name="chshcis"> <ID>1</ID> <Number>1</Number> <Jop>Nothing</Jop> </Employee> </Section> </RootElement>
Mark the "Root Element" above.
You cannot have an XML document like this :
<?xml version="1.0" encoding="utf-8"?> <Section Index="2"> <Employee Name="Micheal"> <ID>1</ID> <Number>1</Number> <Jop>Nothing</Jop> </Employee> </Section>
<Section Index="3"> <Employee Name="chshcis"> <ID>1</ID> <Number>1</Number> <Jop>Nothing</Jop> </Employee> </Section>
because in above xml you have two root elements (Section).
So basically you will have to check if the Document is emoty create a root element first, then start adding the Section elements.
For adding the Child node inside the section element, the last code should work. I suppose you are working with an emprt xmlDoc where i am working with a doc which has some root element .
Let me know if this doesnot work... Send me the XML file details that you are appending these elements to. That should help me.
I hope this helps you...
Regards...
Girija Shankar |
| Girija Shankar Wednesday, August 13, 2008 11:59 PM |
I fix the error and its work fine but your are right it must have one root elemnet so I rebuild my code and add Root name "Sections" then its look like this:
Dim Doc As XmlDocument = New XmlDocument 'Set Xml Document Dim Root As XmlElement = Doc.CreateElement("Sections") 'Set Root Elemnet Dim MainChild As XmlElement = Doc.CreateElement("Section") : MainChild.SetAttribute("index", ComboBox1.SelectedIndex) 'Set Main Child Elemnet Dim Child As XmlElement = Doc.CreateElement("Employee") : Child.SetAttribute("Name", TextBox1.Text) 'Set Child Elemnet Dim SubChild As XmlElement 'Set Sub Child Elemnet Try Doc.Load(Application.StartupPath & "\EmployeeDB.xml") 'Open XML File Catch ex As Exception Dim Dec As XmlDeclaration = Doc.CreateXmlDeclaration("1.0", "utf-8", Nothing) : Doc.AppendChild(Dec) 'Create New XML File End Try SubChild = Doc.CreateElement("ID") Child.AppendChild(SubChild) : SubChild.InnerText = TextBox2.Text SubChild = Doc.CreateElement("Number") Child.AppendChild(SubChild) : SubChild.InnerText = TextBox3.Text SubChild = Doc.CreateElement("Jop") Child.AppendChild(SubChild) : SubChild.InnerText = TextBox4.Text MainChild.AppendChild(Child) Try Root.AppendChild(MainChild) : Doc.AppendChild(Root) 'Add Root & Child Elemnet Catch ex As Exception Doc.SelectNodes("Sections").Item(0).AppendChild(MainChild) 'If Root exist add Child Elemnet only End Try
the resault is:
<?xml version="1.0" encoding="utf-8"?> <Sections> <Section index="2"> <Employee Name="micheal"> <ID>1</ID> <Number>1</Number> <Jop>Nothing</Jop> </Employee> </Section> <Section index="2"> <Employee Name="peter"> <ID>2</ID> <Number>2</Number> <Jop>Nothing</Jop> </Employee> </Section> <Section index="1"> <Employee Name="john"> <ID>3</ID> <Number>3</Number> <Jop>Nothing</Jop> </Employee> </Section> <Section index="1"> <Employee Name="lee"> <ID>4</ID> <Number>4</Number> <Jop>Nothing</Jop> </Employee> </Section> </Sections>
|
| haihtomy Thursday, August 14, 2008 12:43 PM |
Please make sure you mark the answer whichever is correct so that others know that this is resolved
Thanks ..
Regards...
Girija Shankar
|
| Girija Shankar Thursday, August 14, 2008 3:26 PM |
I mark it as answer and if there anyone can help to get the best solution.
last thing can my solution save more than 1000 data? and how about the speed when try to load it for example in listview?
|
| haihtomy Thursday, August 14, 2008 4:41 PM |
You can store more than 1000 data. The speed will be quick.
Hope this helps..
Regards...
Girija Shankar
|
| Girija Shankar Tuesday, August 19, 2008 6:01 AM |