473,668 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XML Writing and Reading

I am using the following code to write to an XML file

myXmlTextWriter .Formatting = System.Xml.Form atting.Indente
myXmlTextWriter .WriteStartElem ent("CPViewer"
myXmlTextWriter .WriteElementSt ring("Height", InputBoxHeight
myXmlTextWriter .WriteElementSt ring("Width", InputBoxWidth
'myXmlTextWrite r.WriteElementS tring("Backgrou nd", (OpenFileDialog 1.FileName)
myXmlTextWriter .WriteElementSt ring("label1.To p", label1.Top
myXmlTextWriter .WriteElementSt ring("label1.Le ft", label1.Left
myXmlTextWriter .WriteElementSt ring("label1.He ight", label1.Height
myXmlTextWriter .WriteElementSt ring("label1.Wi dth", label1.Width
myXmlTextWriter .WriteEndElemen t(
myXmlTextWriter .Flush(
myXmlTextWriter .Close(

Here is the XML output

<CPViewer><Heig ht>600</Height><Width>8 00</Width><label1.T op>200</label1.Top><lab el1.Left>152</label1.Left><la bel1.Height>23</label1.Height>< label1.Width>10 0</label1.Width></CPViewer

Here is the code from the reader

DataSet1.ReadXm l("C:\CPViewer\ LD.xml"
Me.DataBindings .Add(New Binding("Height ", DataSet1, "CPViewer.Heigh t")
Me.DataBindings .Add(New Binding("Width" , DataSet1, "CPViewer.Width ")
Me.DataBindings .Add(New Binding("label1 .Top", DataSet1, "CPViewer.label 1_Top")
'Me.DataBinding s.Add(New Binding("Backgr oundImage", DataSet1, "CPViewer.Backg roundImage")
Me.BackgroundIm age = (Image.FromFile ("C:\CPViewer\B ackgroundImage. jpg")
Me.CenterToScre en(

When I try to read it in will will read the "Height" and "Width" lines and position the form properly but when it reads the "Label1.top " line I get the following error

An unhandled exception of type 'Syste,.Argumen tException' occurred i
system.windows. forms.dl

Additional Information: Cannot bind to property 'label1.Top' on target control

What am I missing here

Thank you
John
Nov 20 '05 #1
12 1478
Hi JCrouse,

I thought I adviced you to use the ds.xmlWrite and ds.xmlRead with that.

I used that method you are now using as well, however that is a lot more
work.

What is the reason you do not want to use that dataset method?

Cor
Nov 20 '05 #2
Hi,

Two things. First Your databinding code was wrong. Second Label1.Top
isn't a valid name use Label1_Top instead.

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

Dim ds As New DataSet

SaveData()
ds.ReadXml("C:\ LD.xml")

Me.DataBindings .Add("Width", ds.Tables(0), "Width")
Me.DataBindings .Add("Height", ds.Tables(0), "Height")
Label1.DataBind ings.Add("Top", ds.Tables(0), "Label1_Top ")
Label1.DataBind ings.Add("Left" , ds.Tables(0), "Label1_Lef t")
Label1.DataBind ings.Add("Width ", ds.Tables(0), "Label1_Wid th")
Label1.DataBind ings.Add("Heigh t", ds.Tables(0), "Label1_Height" )

End Sub

Private Sub SaveData()

Dim myXmlTextWriter As New Xml.XmlTextWrit er("C:\LD.xml" ,
System.Text.Enc oding.Unicode)

myXmlTextWriter .Formatting = System.Xml.Form atting.Indented
myXmlTextWriter .WriteStartElem ent("CPViewer")
myXmlTextWriter .WriteElementSt ring("Height", "600")
myXmlTextWriter .WriteElementSt ring("Width", "800")
'myXmlTextWrite r.WriteElementS tring("Backgrou nd",
(OpenFileDialog 1.FileName))
myXmlTextWriter .WriteElementSt ring("label1_To p", "200")
myXmlTextWriter .WriteElementSt ring("label1_Le ft", "152")
myXmlTextWriter .WriteElementSt ring("label1_He ight", "23")
myXmlTextWriter .WriteElementSt ring("label1_Wi dth", "100")
myXmlTextWriter .WriteEndElemen t()
myXmlTextWriter .Flush()
myXmlTextWriter .Close()
End Sub

Ken
--------------

--
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus (http://www.grisoft.com).
Version: 7.0.230 / Virus Database: 263.0.0 - Release Date: 6/2/2004
Nov 20 '05 #3
Hi John,

Thanks to the message from Ken, I am seeing now what you are doing.
(I did not look that far when I saw the XMLwriter.)

You try to write it using the XMLwriter and read it using the dataset.read
XML
(This part only is for the writing of the dataset, see for the reading Kens
message)

Try this
Dim ds as new dataset("CPView er")
dim dt as new datatable("tabl e1")
ds.tables.add(d t)
dt.columns.add( "Height")
etc.
than you can do
dim dr as new datarow = dt.newrow

dr("Height") = InputBoxHeight. text
etc.

dt.add(dr)
ds.writeXML(pat h)

Maybe it looks more difficult, my expirience is that it is very easy to use.

Cor

Nov 20 '05 #4
Cor....I have no idea what your talking about. You must have me or my post confused with someone else

John
Nov 20 '05 #5
Hi John,

When I hadded sended it I thought that as well sorry, however see my other
post.

Cor
Nov 20 '05 #6
Well Cor....Here's the story. It doesn't seem to like the following line

Dim dr As New DataRow(dt.NewR ow

It tells me that it is not accessable in this context because it is protected

Also, the following line has an error

dt.add(dt

It tells me that "add" is not a member of "system.data.da tatable

What now
Joh

Nov 20 '05 #7
Hi John,
Well Cor....Here's the story. It doesn't seem to like the following line:

Dim dr As New DataRow(dt.NewR ow) (I do really not know why I wrote this, this one does absolute not exist it
has to be)
dim dr as datarow = dt.newrow
Also, the following line has an error:
dt.add(dt)

I think a typo from you because I wrote
ds.add(dt)

I show it better to you

You declare the ds global because you need it probably in more places.
Private ds as dataset

Than in your load event of your form you check using the fileinfo if the
XMLfile exist.

When the file exist you do ds.readXML(file path)

When not you create an empty dataset

ds = new dataset("CPView er")
'this create an empty dataset
dim dt as new datatable("Form 1")
'this create an empty table
ds.tables.add(d t)
'this set a referenence from the dataset to the table, as normaly is said
it places the table in the dataset.
dt.columns.add( "Textbox1Height ")
dt.columns.add( "Textbox2Width" )
'this add columns
You can create a lot of columns
And you create as well a lot of tables just as you like

To fill the items you do,
dim dr as datarow = ds.tables("Form 1").newrow
dr("Height")=2 0
dr("Widht")=21
ds.tables("Form 1").rows.add(dr )

I hope this goes better?

Cor
Nov 20 '05 #8
Cor....That's better. It seems to work now. Here is my code:

Dim ds As New DataSet("CPView er")
Dim dt1 As New DataTable("P1Jo yUp")
Dim dt2 As New DataTable("P1Jo yLeft")
ds.Tables.Add(d t1)
dt1.Columns.Add ("Height")
dt1.Columns.Add ("Width")
ds.Tables.Add(d t2)
dt2.Columns.Add ("Height")
dt2.Columns.Add ("Width")

Dim dr1 As DataRow = ds.Tables("P1Jo yUp").NewRow
dr1("Height") = lblP1JoyUp.Heig ht
dr1("Width") = lblP1JoyUp.Widt h
Dim dr2 As DataRow = ds.Tables("P1Jo yLeft").NewRow
dr2("Height") = lblP1JoyLeft.Wi dth
dr2("Width") = lblP1JoyLeft.Wi dth

ds.Tables("P1Jo yUp").Rows.Add( dr1)
ds.Tables("P1Jo yLeft").Rows.Ad d(dr2)

ds.WriteXml("C: \CPViewer\CPVie wer.XML")

Now, how am I going to handle writing out the fore color, back color and font of the labels and the background image of the form? This was the part that gave me trouble before. Keep it simple, I'm a n00b.

Thank for your help,
John

"Cor Ligthert" wrote:
Hi John,
Well Cor....Here's the story. It doesn't seem to like the following line:

Dim dr As New DataRow(dt.NewR ow)

(I do really not know why I wrote this, this one does absolute not exist it
has to be)
dim dr as datarow = dt.newrow
Also, the following line has an error:
dt.add(dt)

I think a typo from you because I wrote
ds.add(dt)

I show it better to you

You declare the ds global because you need it probably in more places.
Private ds as dataset

Than in your load event of your form you check using the fileinfo if the
XMLfile exist.

When the file exist you do ds.readXML(file path)

When not you create an empty dataset

ds = new dataset("CPView er")
'this create an empty dataset
dim dt as new datatable("Form 1")
'this create an empty table
ds.tables.add(d t)
'this set a referenence from the dataset to the table, as normaly is said
it places the table in the dataset.
dt.columns.add( "Textbox1Height ")
dt.columns.add( "Textbox2Width" )
'this add columns
You can create a lot of columns
And you create as well a lot of tables just as you like

To fill the items you do,
dim dr as datarow = ds.tables("Form 1").newrow
dr("Height")=2 0
dr("Widht")=21
ds.tables("Form 1").rows.add(dr )

I hope this goes better?

Cor

Nov 20 '05 #9
Hi John,

You mean something as this
\\\
Dim label1fontcolor As String = Me.Label1.ForeC olor.ToString
Dim label1font As String = Me.Label1.Font. ToString
Dim label1backcolor As String = Me.Label1.BackC olor.ToString
///
You have to extract them of course to use them again, have as well a look at
the split for that.

I saw in other messages you are not using the background image as set in the
resource so I assume that is just saving the path.

I hope this helps?

Cor
Cor....That's better. It seems to work now. Here is my code: <...> Now, how am I going to handle writing out the fore color, back color and

font of the labels and the background image of the form? This was the part
that gave me trouble before. Keep it simple, I'm a n00b.
Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3932
by: Google Mike | last post by:
Tell me if this can be done, and if I have a misconception here. I am writing an app that will be served up in an app farm, and therefore I need to move the session information to the client, not using server-side session features. The way I have typically done this in the ASP world was with cookies. Another way to handle this is with a keyed record in a database, but the cookie strategy reduces the number of times I need to hit a...
6
23593
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
4
2079
by: Mark Stijnman | last post by:
A while ago I posted a question about how to get operator behave differently for reading and writing. I basically wanted to make a vector that can be queried about whether it is modified recently or not. My first idea, using the const and non-const versions of operator, was clearly not correct, as was pointed out. Julián Albo suggested I could use proxies to do that. I've done some googling for proxies (also in this group) and personally,...
9
2930
by: 100 | last post by:
Has anybody read Steve Maguire's book "Writing solid code"? Do you think that the ideas in this book are not applicable in c# language? Does anybody find if(2 == i) istead of if(i == 2) as unnetural and does it lead to more bugs in the code because of it makes programms hard to read. And my last question is: "Do you think that using boolean expressions
2
2007
by: John Salerno | last post by:
I wrote this code just to experiment with writing to and reading from a file. It seems to work fine when writing, but when reading the file, it only prints the filepath to the screen, not the file contents. System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\positions.txt"); System.IO.StringReader myFile = new System.IO.StringReader(@"C:\positions.txt"); for (int i = 0; i < 3; i++)
5
5066
by: UJ | last post by:
I have a system that has five programs that all communicate with each other via Message Queues. Works well. One program is a watchdog that will make sure the others are up and going. Currently I have it store info it gets from when the programs check in into a DataSet (XML file). Problem is, that file now has to be used by other programs to find out version information (the file is ALWAYS less that 1K.) The record itself is only five fields...
16
7176
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain unchanged). How can I do that in Python? Claudio Grondi
6
5262
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
3
2299
by: fuenfzig | last post by:
Hi all, I want to use a single std::stringbuf for writing (by a std::ostream) and for reading (by a std::istream), concurrently in two threads. This came to my mind, because the code for reading should be as with any other istream and cannot be changed. Now my problem is, that a do not get any data in the reading loop std::ofstream tempfile("test.pdf", std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);...
42
4847
by: psbasha | last post by:
Hi, Is it necessary in Python to close the File after reading or writing the data to file?.While refering to Python material ,I saw some where mentioning that no need to close the file.Correct me if I am wrong. If possible could anybody help me with sample code for reading and writing a simple text file.I have seen there are many ways to read /write the data in Python.But I want to use the effective way of reading or writing the data...
0
8462
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8382
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8586
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1787
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.