|
-
Oct 10th, 2003, 06:05 PM
#1
Thread Starter
Frenzied Member
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>
-
Oct 10th, 2003, 11:21 PM
#2
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:
Dim doc As New Xml.XmlDocument
'load file
doc.Load("..\sample.xml")
'get root node named users
Dim Usersnode As Xml.XmlElement = doc.SelectSingleNode("//users")
'add the new node
Dim newNode As Xml.XmlElement = doc.CreateElement("user")
'add attributes
newNode.SetAttribute("name", "Edneeis")
newNode.SetAttribute("active", True)
'add children nodes if any
Dim child As Xml.XmlElement = doc.CreateElement("first")
child.InnerText = "Ed"
newNode.AppendChild(child)
child = doc.CreateElement("last")
child.InnerText = "Marquez"
newNode.AppendChild(child)
child = doc.CreateElement("address")
child.InnerText = "15420 Bello Way"
newNode.AppendChild(child)
child = doc.CreateElement("city")
child.InnerText = "Moreno Valley"
newNode.AppendChild(child)
child = doc.CreateElement("state")
child.InnerText = "CA"
newNode.AppendChild(child)
child = doc.CreateElement("zip")
child.InnerText = "92555"
newNode.AppendChild(child)
'add new node to users node
Usersnode.AppendChild(newNode)
'save doc
doc.Save("..\sample.xml")
-
Oct 11th, 2003, 12:10 AM
#3
Thread Starter
Frenzied Member
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.
-
Oct 11th, 2003, 12:17 AM
#4
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.
-
Oct 11th, 2003, 12:28 AM
#5
Thread Starter
Frenzied Member
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
-
Oct 11th, 2003, 12:35 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|