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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read an XML file into textboxes and then update its values

Status
Not open for further replies.

hamutoff

Programmer
Feb 26, 2008
5
0
0
US
Hello! ...how do I read an XML file and then update its values. What I would like to do is load data from an XML file onto fields on my form. I would then like to type in new values into these fields and then update the values in the XML file.

I already have my form displaying the desired values in text boxes.

I want to provide a function to write the changes to the xml file.

I dynamically create the textboxes; one for each element that needs to be displayed, then updated. I am only dealing with text and comment nodes at this time. (only want to update the text nodes.)

The created text boxes hold a few values; the text (value to be updated), and I have the xml LinePosition, Depth, and LineNumber of the associated xml "record" in question.

The text is displayed in the dynamic textboxes, the other values are accessible (parsed within the name etc)

Matt Hamutoff
 
Example of possible xml file my app is to deal with...
Code:
<?xml version="1.0" encoding="utf-8"?>
<Application>
  <Connections>
    <database name="PSIGLOBAL" dbtype="1">Server=(LOCAL);Database=PSIGLOBAL;</database>
  </Connections>
  <PsiGlobal>
    <!-- This setting for the PSI WEB PORTAL APPLICATION -->
    <setting name="Portal:EmailSendTo">matt@progressivesolutions.com</setting>
    <!-- This setting number of active requets allowed per login/user name -->
    <setting name="Portal:ActiveRequestsAllowed">3</setting>   
  </PsiGlobal>
</Application>
This is the output from my XmlTextReader I use to build and populate my dynamic text boxes
Code:
Name: 
Attribute Count: 0
Depth: 3
Line Number: 4
Line Position: 43
Node Type: Text
ValueType: System.String
Has Value: True
Value: Server=(LOCAL);Database=PSIGLOBAL; 
=====================
Name: 
Attribute Count: 0
Depth: 2
Line Number: 8
Line Position: 9
Node Type: Comment
ValueType: System.String
Has Value: True
Value:  This setting for the PSI WEB PORTAL APPLICATION  
=====================
Name: setting
Attribute Count: 1
Depth: 2
Line Number: 9
Line Position: 6
Node Type: Element
ValueType: System.String
Has Value: False
Value:  Portal:EmailSendTo
=====================
Name: 
Attribute Count: 0
Depth: 3
Line Number: 9
Line Position: 40
Node Type: Text
ValueType: System.String
Has Value: True
Value: matt@companyname.com 
=====================

BTW this is all quite new to me :)... I have spent a few hours searching for a solution. I want to add, am trying to make the app flexible being able to read write XML files from various sources. THANK YOU!!!
 
I have been struggling for a few days with my posted problem and dont have a solution to strive towards yet. In 'researching' via the web I turned up a few ideas, could you all debunk them for me please?

Combining the XmlReader and XmlWriter classes for simple streaming transformations

as written about:

This makes me think that I might be able to use XmlTextReader - thus finding my pre-specified area (by way of the form textboxes) and write to the xml document at that moment; is this possible? The below seems to indicate that it might be?::

I also read: XmlTextReader and XmlTextWriter support reading data to/from text-based stream, while XmlNodeReader and XmlNodeWriter are designed for working with in-memory DOM tree structure. The custom readers and writers can also be developed to extend the built-in functionality of XmlReader and XmlWriter.
 
Please any hints anybody out there? I have still been trying but could use the ideas of other minds.....

Thank you
 
I'll jump out on a limb here and give it a shot. I've done something similar in Visual FoxPro and C# where the user updates settings from an external XML file. I keep the DOM as an object property from which I read and write each XML node or element as needed. If you know the node name then it's pretty easy to get a handle to the containing node via DOM.SelectSingleNode("xpathvalue") and then just change the node's value. Since the node is still part of the DOM the document's XML is changed as well. For my purposes when the form closes I just re-save the DOM to my file.

Hope that makes sense and helps a little.

Ralph Kolva
 
Thank you for your response. I apprecaiate it, perhaps what I wish to do is not possible

MUST one know the node name to write to it? Are there ANY other attributes that may be used to identify the node to write to?

I want to write to the node by other means. WHY are there these other attributes if they cannot be used?
Depth
Line Number
Line Position

It is possible that the node name could be the same but these above attributes are specific and unique like GPS coordinates! They seem to beg to be used since they are so easy to get when reading!

Also what do I do when there are multiple elements in a node say 3 and I only want to read and write to the one in the middle?

Does this make sense?
 
I would bet you could create an xpath expression to get a node by value but I'm not experienced enough to tell you how to do that. Seems like you would need to hold on to the old value in order to find the node and then change it's value to the new value

I supposed you could traverse the XML by depth by stepping through child and sibling nodes but seems more difficult and problematic if the xml changes structure, adding or dropping a node.

Given the line and position numbers you could string parse it but seems like you would need to know where the following node in the text ends, again seems like a lot of work to me. Might be easy enough if you do a string replace one value for another but again you would need to hold on to the old value long enough to do that.

As I said, I'm not an expert on xml, as some in this forum are, but I generally find it easier to think of the xml as a tree object (DOM) and manipulate it referring to element and attribute objects (nodes).

Ralph Kolva
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top