Click here to Skip to main content
16,004,778 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have Xml File

XML
<?xml version="1.0" encoding="utf-8"?>
<Details>
    <DATA>
        <Signature_Key>132</Signature_Key>
        <License_Key>54</License_Key>
        <Company_Name>Test Automation Services</Company_Name>
        <Machine_Name>Test-PC</Machine_Name>
        <Date_And_Time>13/09/2013 5:46:19 PM</Date_And_Time>
    </DATA>
</Details>


Now, I want to add One more data which are user manually insert from textbox,
So , i am searching for function, in which I pass all value (Xml path,sign_key, Licen_key, Cmpan_name, machinename) as
parameter and I can add one more data in existing XMl.

I try this function, But its showing Error.. Kindly help me!!

C#
Public Sub AppendData(ByVal FilePath As String, ByVal signature As String, ByVal Licencekey As String, ByVal ComapnyNam As String, ByVal Machine_Name As String)


        Dim document As XDocument

        If File.Exists(FilePath) Then
            Using stream As IsolatedStorageFileStream = File.Open(FilePath, FileMode.Open)
                document = XDocument.Load(stream)
            End Using
            Stream.Close()
        Else
            document = XDocument.Load(FilePath)
        End If

        Dim root = New XElement("book")
        Dim sign = New XElement("Signature_Key", signature)
        Dim license = New XElement("License_Key", Licencekey)
        Dim comName = New XElement("Company_Name", ComapnyNam)
        Dim MachName = New XElement("Machine_Name", Machine_Name)
        Dim _Date = New XElement("Date_And_Time", DateAndTime.Now)



        root.Add(sign, license, comName, MachName, _Date)
        document.Root.Add(root)



        Using stream As IsolatedStorageFileStream = File.Create(FilePath)
            document.Save(FilePath)
        End Using





    End Sub
Posted
Comments
Maciej Los 13-Sep-13 8:40am    
What kind of error?

Assuming you have the original document:

VB
var doc = XDocument.Load(...);

then create a new element (not a document)
VB
var newElement =  new XElement("books",
                   new XElement("Signature_Key", Signature),
                   new XElement("License_Key", Licencekey),
                   new XElement("Company_Name", ComapnyNam),
                   new XElement("Machine_Name", Machine_Name);
                   new XElement("Date_And_Time", DateAndTime.Now);

And then insert it:
VB
doc.Element("stock").Add(newElement);

doc.Save(....);
 
Share this answer
 
v2
Comments
Varun_nayak 14-Sep-13 1:02am    
Thanks @salman your answer help a lot to find actual solution
this is the right solution, I get with help of @salman622



VB
Public Sub AppendData(ByVal FilePath As String, ByVal signature As String, ByVal Licencekey As String, ByVal ComapnyNam As String, ByVal Machine_Name As String)


        Dim document As XDocument
        If File.Exists(FilePath) Then
            document = XDocument.Load(FilePath)
        Else
            MessageBox.Show("No such file Exists'")
        End If
        Dim root = New XElement("Data")
        Dim sign = New XElement("Signature_Key", signature)
        Dim license = New XElement("License_Key", Licencekey)
        Dim comName = New XElement("Company_Name", ComapnyNam)
        Dim MachName = New XElement("Machine_Name", Machine_Name)
        Dim _Date = New XElement("Date_And_Time", DateAndTime.Now)
        root.Add(sign, license, comName, MachName, _Date)

        document.Root.Add(root)
        document.Save(FilePath)



    End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900