Solved

vb.net XML edit in form..

Posted on 2010-08-19
11
222 Views
Last Modified: 2012-05-10
Hello.. I have some problem. Start from the beginning..

I have XML-File


looks like this...


<?xml version="1.0" encoding="utf-8"?>
<tagdata>
  <readopcvalues>
    <value opctag="TEST.AI_Weight_KL3" />
    <value opctag="TEST._produktion" />
    <value opctag="SystemVariables.ServerTime" />
  </readopcvalues>
</tagdata>



What I would like to do first is have the change to add some new tags to my taglist..

tex. add this..

<value opctag="TEST.Goood" />


<?xml version="1.0" encoding="utf-8"?>
<tagdata>
  <readopcvalues>
    <value opctag="TEST.AI_Weight_KL3" />
    <value opctag="TEST._produktion" />
    <value opctag="SystemVariables.ServerTime" />
    <value opctag="TEST.Goood" />
  </readopcvalues>
</tagdata>




I have done this with this code but dosent get really waht I want.. The problem is that I always starts a new nodes and not write in the old one...

look


<?xml version="1.0" encoding="utf-8"?>
<tagdata>
  <readopcvalues>
    <value opctag="TEST.AI_Weight_KL3" />
    <value opctag="TEST._produktion" />
    <value opctag="SystemVariables.ServerTime" />
   </readopcvalues>
  <value opctag="TEST.Goood" />
</tagdata>




This is the code...

   '-- Declare local variable and load xml file
        Dim doc2 As New XmlDocument()
        doc2.Load("XML\TagData.xml")

        '-- Create a new node and add it to the document.
        Dim newline As XmlElement = doc2.CreateElement("value")

        newline.SetAttribute("opctag", "PLC.Beijer_Agnesberg.OP_Dagens_produktion")

        doc2.DocumentElement.AppendChild(newline)
        '-- Save the xml file with the new changes.
        doc2.Save("XML\TagData.xml")



Please give me some tips.. My follow question is that I wolud like to show the Whole XML lines in textboxes..
like this.

textbox1.text =  <value opctag="TEST.Goood" />
textbox2.text =    <value opctag="TEST.AI_Weight_KL3" />

Why I want this it´s because I wolud like to edit the old line.. or add a new line.. or remove one..
I hope someone understand! But first we take the easy party to write in the same nod! :D

//Kavvis
0
Comment
Question by:kavvis
  • 6
  • 5
11 Comments
 
LVL 27

Expert Comment

by:nmarun
ID: 33474452
0
 

Author Comment

by:kavvis
ID: 33474483
Arun  thank you but I really dont know how to use this info into my code....
any other idees?
0
 
LVL 27

Expert Comment

by:nmarun
ID: 33474842
This is what I'm talking.

Arun

private static void UpdateXml()

{

    string xmlPath = @"C:\Arun\Projects\ConsoleTest\ConsoleTest\Response.xml";



    DataSet dataSet = new DataSet();

    dataSet.ReadXml(xmlPath);

    dataSet.AcceptChanges();

    // <value opctag="TEST.Goood" />

    DataRow readopcvaluesRow = dataSet.Tables["readopcvalues"].Rows[0];

    DataRow newRow = dataSet.Tables["value"].NewRow();

    newRow["opctag"] = "Test.Gooood";

    dataSet.Tables["value"].Rows.Add(newRow);

    newRow.SetParentRow(readopcvaluesRow);

    dataSet.WriteXml(xmlPath, XmlWriteMode.IgnoreSchema);

}

Open in new window

0
 
LVL 27

Expert Comment

by:nmarun
ID: 33474852
And here's the vb.net version.

Arun

Private Shared Sub UpdateXml()

	Dim xmlPath As String = "C:\Arun\Projects\ConsoleTest\ConsoleTest\Response.xml"



	Dim dataSet As New DataSet()

	dataSet.ReadXml(xmlPath)

	dataSet.AcceptChanges()

	' <value opctag="TEST.Goood" />

	Dim readopcvaluesRow As DataRow = dataSet.Tables("readopcvalues").Rows(0)

	Dim newRow As DataRow = dataSet.Tables("value").NewRow()

	newRow("opctag") = "Test.Gooood"

	dataSet.Tables("value").Rows.Add(newRow)

	newRow.SetParentRow(readopcvaluesRow)

	dataSet.WriteXml(xmlPath, XmlWriteMode.IgnoreSchema)

End Sub

Open in new window

0
 

Author Comment

by:kavvis
ID: 33474956
Arun

OJ!! Thank you! Verry mutch!
It works greate!

Can I somehow remove on of lines tags in some simple way to?

I will give you the points just want to know some more info if you have before I close it! :D
/kavvis

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 27

Expert Comment

by:nmarun
ID: 33474960
And here's the second part of your question.

Arun

Private Shared Sub ReadInnerXml()

	Dim xmlPath As String = "C:\Arun\Projects\ConsoleTest\ConsoleTest\Response.xml"



	Dim xDocument__1 As XDocument = XDocument.Load(xmlPath)



	Dim valueXmlList As List(Of String) = (From value In xDocument__1.Descendants("value")value.ToString()).ToList()



	For i As Integer = 0 To valueXmlList.Count - 1

		Console.WriteLine(valueXmlList(i))

	Next

End Sub

Open in new window

0
 

Author Comment

by:kavvis
ID: 33474981
nmarun:  I can´t really get that to work my friend...
0
 
LVL 27

Accepted Solution

by:
nmarun earned 500 total points
ID: 33475014
Try this.

Arun

Private Shared Sub RemoveXmlNode()

	Dim xmlPath As String = "C:\Arun\Projects\ConsoleTest\ConsoleTest\Response.xml"



	Dim dataSet As New DataSet()

	dataSet.ReadXml(xmlPath)

	dataSet.AcceptChanges()

	' <value opctag="TEST.Goood" />



	For i As Integer = 0 To dataSet.Tables("value").Rows.Count - 1

		If dataSet.Tables("value").Rows(i)("opctag").Equals("Test.Gooood") Then

			dataSet.Tables("value").Rows(i).Delete()

			dataSet.Tables("value").Rows(i).AcceptChanges()

		End If

	Next



	dataSet.WriteXml(xmlPath, XmlWriteMode.IgnoreSchema)

End Sub

Open in new window

0
 
LVL 27

Expert Comment

by:nmarun
ID: 33475023
What are you getting?

Arun
0
 

Author Comment

by:kavvis
ID: 33475100
Sorry it was my bad!
that line worked perfekt!

Can I ask one more question..

If I want to show   " <value opctag="Test.Gooood" /> "  in a textbox

just to show all the lines... so I can se what the diffrent rows I have...
so I can delete or replace one of the line easy...

How do I do that the best way?

I have tried to make the whole XML dokumet to a long string and then
like this

textbox1.text = doc.tostring.substring(0.10)
textbox2.text = doc.tostring.substring(23.34)

but that dosent work.. so please tips me then I can close this question and give booth points! :D
0
 

Author Closing Comment

by:kavvis
ID: 33475646
Thank you!
everythin works very well now!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vb.net 2 37
vb.net checkbox 7 41
Duplicating a tab page content to another in TabControl control 11 40
Not seen Link button 5 14
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now