Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add nodes to xml document 1

Status
Not open for further replies.

colin81

Technical User
Jul 22, 2002
77
GB
Hi All

First of all happy new year!

Im very new to vb.net and Im struggling to add data to an existing xml document. An example of the document is as below :

-----------------------------------------------------------

<?xml version="1.0" encoding="iso-8859-1"?>
<tasks>
<task >
<employeeName>Bob</employeeName>
<taskDescription> <taskCreationDate>01/06/2004</taskCreationDate>
<completionDate>01/06/2004</completionDate>
<status>Completed</status>
</task>
<task >
<employeeName>Bob</employeeName>
<taskDescription> <taskCreationDate>01/06/2004</taskCreationDate>
<completionDate>01/06/2004</completionDate>
<status>Completed</status>
</task>
</tasks>

-----------------------------------------------------------

I wish to add a new task to the document, can anyone help?

Many Thanks for reading my question
Colin
 
Hi,

How do you want to add the new task ?
Manually in the XML-document ?
From where does the document come ? A database ?
Do you have the xml-schema ?
 
Hi,

Thanks for replying. I wish to add the task from code not manually (plan to do this from a web service). The document does not come from a database, it is just a standard xml document for storing tasks in. if it is possible to validate the xml against a schema from code that would be good also, A schema is below (although the declaration is not in the above xml document).

---------------------------------------------------------

<?xml version="1.0" ?>
<xs:schema id="tasks" targetNamespace=" xmlns:mstns=" xmlns=" xmlns:xs=" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:element name="tasks" msdata:IsDataSet="true" msdata:Locale="en-GB" msdata:EnforceConstraints="False">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="task">
<xs:complexType>
<xs:sequence>
<xs:element name="employeeName" type="xs:string" minOccurs="0" />
<xs:element name="taskDescription" type="xs:string" minOccurs="0" />
<xs:element name="taskCreationDate" type="xs:date" minOccurs="0" />
<xs:element name="completionDate" type="xs:date" minOccurs="0" />
<xs:element name="status" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

---------------------------------------------------------
 
all the referances ive seen
1. load document into a dataset.
2. attach dataset to data grid
3. make changes (add delete modify) data grid
4. save data grid to xmldocument

i havent found anything that allows one record to be added using random access of an xml document.


if it is to be it's up to me
 
I don't know i understand you.
But if i had to do this, i should write some simple code to store it in an access database.
From the database you can retrieve your xml-document back.

I choose my username Tsunami so that everytime somebody see this, he or she will remember this disaster where i lost a good friend.
 
Is it not possible to traverse a list of nodes within an xml document or append the list of tasks ? I think it is in the xml dom but am unsure how to do it in .net.....

tsunami100 : I do not wish to store data within an access database, I wish to write (append) an xml document....no database transactions need to be involved

Inifnitelo : I do not believe you have to involve a datagrid to write to an xml document
 
XML is usually stored in memory inside a XmlDocument object (from System.Xml namespace).

If you have your XML in a string, you would call .LoadXml() on your XmlDocument, which would read the string and construct an in-memory representation of your XML. You can then create another task node with code like this:
Code:
  Dim TasksElem As XmlElement
  Dim TaskElem As XmlElement
  Dim TempElem As XmlElement

  ' Find where to put the new task element in the doc
  TasksElem = MyDoc.ChildNodes(0)

  ' Create new child element & append to tasks element
  TaskElem = MyDoc.CreateElement("task")
  TasksElem.AppendChild(TaskElem)

  ' create & append the data elements
  TempElem = MyDoc.CreateElement("employeeName")
  TempElem.InnerText = "Bob"
  TaskElem.AppendChild(TempElem)

  TempElem = MyDoc.CreateElement("taskDescription")
  TempElem.InnerText = "[URL unfurl="true"]wwwww"[/URL]
  TaskElem.AppendChild(TempElem)
  
  TempElem = MyDoc.CreateElement("taskCreationDate")
  TempElem.InnerText = "01/06/2004"
  TaskElem.AppendChild(TempElem)

  TempElem = MyDoc.CreateElement("completionDate")
  TempElem.InnerText = "01/06/2004"
  TaskElem.AppendChild(TempElem)

  TempElem = MyDoc.CreateElement("status")
  TempElem.InnerText = "Completed"
  TaskElem.AppendChild(TempElem)
Once you have it in a XmlDocument, you can convert it back to a string with the Save() method to one of: String, Stream, TextWriter, or XmlWriter.

You should note that your dates are in the wrong format for XML. You should use the ISO-8601 format, and .NET has a class to help you with this, especially when you're starting with a .NET DateTime datatype:
Code:
  TempElem = MyDoc.CreateElement("completionDate")
  Dim CompletionDate As New DateTime(2004, 1, 6)
  TempElem.InnerText = XmlConvert.ToString(CompletionDate)
  TaskElem.AppendChild(TempElem)
Going the other way is easy too:
Code:
  Dim CompletionDate As DateTime
  CompletionDate = XmlConvert.ToDateTime(TempElem.InnerText)
Chip H.



____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks chiph that was really helpful and worked straight away!

One further thing can I validate the data input against a schema?

A star for you for your efforts! Once again , Many Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top