Results 1 to 6 of 6

Thread: Write to an existing XML file

  1. #1

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141

    Write to an existing XML file

    Is there a simple way to write to an existing XML file, but not to the end of it? Below is the XML I have, and I want to add a new <user></user>. But, I want that user in the <users></users> block. Everything I have been doing so far is putting the new element after the <groups></groups> block.
    Code:
    <?xml version="1.0" encoding="utf-8" ?> 
    <security>
    	<users>
    		<user name="EmployeeA" active="True">
    			<first>Employee</first>
    			<last>A</last>
    			<email>[email protected]</email>
    			<address>1234 Here St.</address>
    			<city>Mesa</city>
    			<state>AZ</state>
    			<zip>85205</zip>
    		</user>
    		<user name="EmployeeB" active="False"></user>
    		<user name="ClientA" active="True"></user>
    		<user name="ClientB" active="True"></user>
    	</users>
    	<groups>
    		<group name="Employees" description="Default Group to hold all users that are employees">
    			<user>EmployeeA</user>
    			<user>EmployeeB</user>
    		</group>
    		<group name="Clients" description="Default Group to hold all users that are clients">
    			<user>ClientA</user>
    			<user>ClientB</user>
    		</group>
    		<group name="AllNonAdminUsers" description="Group containing all users that are not administrators">
    			<group>Employees</group>
    			<group>Clients</group>
    		</group>
    	</groups>
    </security>
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    What methods did you try before? Sounds like a dataset maybe. Datasets are great because they are easy and take little code, but unfortunately they see the table structure only and not the hierarchy. If you use an XMLDocument then you'll be all set here is an example that will work with your example XML.
    VB Code:
    1. Dim doc As New Xml.XmlDocument
    2.         'load file
    3.         doc.Load("..\sample.xml")
    4.         'get root node named users
    5.         Dim Usersnode As Xml.XmlElement = doc.SelectSingleNode("//users")
    6.         'add the new node
    7.         Dim newNode As Xml.XmlElement = doc.CreateElement("user")
    8.         'add attributes
    9.         newNode.SetAttribute("name", "Edneeis")
    10.         newNode.SetAttribute("active", True)
    11.         'add children nodes if any
    12.         Dim child As Xml.XmlElement = doc.CreateElement("first")
    13.         child.InnerText = "Ed"
    14.         newNode.AppendChild(child)
    15.         child = doc.CreateElement("last")
    16.         child.InnerText = "Marquez"
    17.         newNode.AppendChild(child)
    18.         child = doc.CreateElement("address")
    19.         child.InnerText = "15420 Bello Way"
    20.         newNode.AppendChild(child)
    21.         child = doc.CreateElement("city")
    22.         child.InnerText = "Moreno Valley"
    23.         newNode.AppendChild(child)
    24.         child = doc.CreateElement("state")
    25.         child.InnerText = "CA"
    26.         newNode.AppendChild(child)
    27.         child = doc.CreateElement("zip")
    28.         child.InnerText = "92555"
    29.         newNode.AppendChild(child)
    30.         'add new node to users node
    31.         Usersnode.AppendChild(newNode)
    32.         'save doc
    33.         doc.Save("..\sample.xml")

  3. #3

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141
    Thanks for that!

    How would I have to change my XML to work best with a DataSet? What I am trying to do is move my data into XML files (so I am messing around now trying to get the hang of them), so that I am database independent with my applications. I would like to eventually make a class that pulls data out of a database, and into XML, so that the rest of my applications can just work with XML. This way when I create a new application it doesn't matter what the datasource is because it will just have to use XML.
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can use it from a dataset and it'll work fine but it wont place all the user items under the user node. A dataset sees the data as tables the same as if you load the xml file into the IDE and look at it in Data view instead of XML view. It will still work fine though. You can load a Dataset with data via a dataadapter from any datasource and just use WriteXML to write it out as XML. Another point is that if you only use the dataset to read the xml then it shouldn't matter if the user node is in the users node or not other than for looks. If heirarchy matters then you'll need to use an XMLDocument or XPathNavigator or XQuery. I have never used an XPathNavigator before and XQuery is kind of still in development but it looks promising.

    A side note on the children nodes (first,last,address,city..) to user you'd probably need to add some sort of id field because when you look at it as a big table it may not tie back to a specific user. Again or not use them in a dataset.

  5. #5

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141
    Ok, so I could have a project that connects via whatever datasource is needed for each database, and just write it to XML? Then use that XML generated for the applications? That sounds easier than what I was going to do
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Yup. Once the data is in the dataset (however you want to get it there) then you can export it to an XML in one method call:
    MyDataSet.WriteXML(filepathhere)

    You can do more than that really but that is the jist.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width