Confusion in XML
Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=6911
Printed Date: 29 March 2026 at 2:09pm Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com
Topic: Confusion in XML
Posted By: Diep-Vriezer
Subject: Confusion in XML
Date Posted: 01 November 2003 at 3:28pm
|
Hi again,
This time I'm trying to write XML logfiles for my web application. The VS.NET help guide confuses me by suplying me with tons of code wich doesn't make sense at all.
So I wanted to know if anyone knows howto write a simple XML file like this:
<NewEntry sessionId="<session>"> <Entry name="EntryText">This is a sample entry</Entry> <Entry name="Client IP">0.0.0.0</Entry> </NewEntry>
Reading it is another thing, but this is the main part, since writing and reading is almost the same (according to VS.NET). Anyone have any ideas?
------------- Gone..
|
Replies:
Posted By: MorningZ
Date Posted: 01 November 2003 at 4:41pm
http://www.morningz.com/sections/playground/files/code/NETGallery.zip - check out the code i wrote that'll create an XML document from scratch
------------- Contribute to the working anarchy we fondly call the Internet
|
Posted By: Diep-Vriezer
Date Posted: 01 November 2003 at 4:45pm
|
Thanks 
I'll have a look
------------- Gone..
|
Posted By: MorningZ
Date Posted: 01 November 2003 at 4:47pm
dont forget that you must give the web server "write" permissions in the directory
------------- Contribute to the working anarchy we fondly call the Internet
|
Posted By: Diep-Vriezer
Date Posted: 01 November 2003 at 5:38pm
I know! But hey, it works perfect! I'm just figuring out howto ADD the data to a sheet, but this is probably done using some File.AppendText method or something. Anyway, I'm really now, it's about 1:45 now, so I'd better go and sleep.. z Z z Z Z z Z z Z z z
------------- Gone..
|
Posted By: Diep-Vriezer
Date Posted: 02 November 2003 at 4:38am
Uhhm, I can't figure out howto append XML to a existing sheet.
------------- Gone..
|
Posted By: Diep-Vriezer
Date Posted: 02 November 2003 at 4:57am
Is there even a way of doing this? If not, it isn't really handy to use as a logfile..
------------- Gone..
|
Posted By: MorningZ
Date Posted: 02 November 2003 at 5:27pm
Diep-Vriezer wrote:
Uhhm, I can't figure out howto append XML to a existing sheet. |
look in the "AppendDocData" subroutine in my ascx file
------------- Contribute to the working anarchy we fondly call the Internet
|
Posted By: dpyers
Date Posted: 06 November 2003 at 9:30pm
|
MorningZ...
Nice piece of code
-------------
Lead me not into temptation... I know the short cut, follow me.
|
Posted By: MorningZ
Date Posted: 07 November 2003 at 5:02am
|
thanks..
it could be a little cleaner/less-lines-of-code than it is.. but i wrote that last year when i was just starting to mess around with .NET code.....
------------- Contribute to the working anarchy we fondly call the Internet
|
Posted By: Diep-Vriezer
Date Posted: 23 November 2003 at 3:25pm
One tiny little more thing > Updating. A config file hasn't got much use if you can't update it, and I was wondering if there was any way of doing so.
------------- Gone..
|
Posted By: Mart
Date Posted: 24 November 2003 at 9:53am
|
You could select the element using xpath, save all the data to a variable or something then append the element with the old data and the changed variable.
|
Posted By: MorningZ
Date Posted: 24 November 2003 at 10:03am
|
i think people are losing fact that XML used to store data, such as in config file, shouldn't be treated as a text file, but rather as a representation of data...
you can add/remove/update items/parameters/nodes/whatever just like you were updating records in a database, its just a matter of coding syntax....
but for sure one's gotta start with getting out of the "text file/File System Object" thinking
------------- Contribute to the working anarchy we fondly call the Internet
|
Posted By: Diep-Vriezer
Date Posted: 24 November 2003 at 12:53pm
So how should you think.. If you see XML as a database, which is somesort of is, it's still very new to me, so I'll download some e-book about it.
------------- Gone..
|
Posted By: MorningZ
Date Posted: 24 November 2003 at 1:36pm
|
There is no difference between going to the database and filling a DataSet, like: Dim objConn As new OleDBConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db\this.mdb") Dim objCmd As new OleDbDataAdapter("SELECT * FROM SITE_Config", objConn) Dim ds As DataSet = new DataSet() objCmd.Fill(ds,"tblUsers")
or using the "LoadXML" method to fill a DataSet, like: Dim dsImageInfo as New DataSet() dsImageInfo.ReadXml( CheckForXMLFile() )
once its in the DataSet, you can manipulate, go through and find, change, add data just like any record set ( http://www.4guysfromrolla.com/webtech/chapters/ASPNET/ch06.6.shtml - this article shows some syntax in the "Building a DataTable Programmatically" section ), and at the end, there is a http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatasetclasstopic.asp - "WriteXML" method to resave the file
------------- Contribute to the working anarchy we fondly call the Internet
|
Posted By: Diep-Vriezer
Date Posted: 25 November 2003 at 5:17am
I know, but I'm not really into datasets.
------------- Gone..
|
Posted By: MorningZ
Date Posted: 25 November 2003 at 5:49am
|
you asked "how" above my reply, i gave you an answer (although thats not the only way, just the easiest to understand from what i have run across)...
your option to "get into" datasets and making working with XML more .NET-like and less TextFile-like 
------------- Contribute to the working anarchy we fondly call the Internet
|
Posted By: Diep-Vriezer
Date Posted: 25 November 2003 at 10:23am
|
I'm working on it! I've build a website which completely runs on a Site.config file, so I can read it, but updating is a different story .
------------- Gone..
|
Posted By: MorningZ
Date Posted: 26 November 2003 at 10:01am
|
heh, talk about timing :)
http://aspnet.4guysfromrolla.com/articles/112603-1.aspx - http://aspnet.4guysfromrolla.com/articles/112603-1.aspx
for sure check it out
------------- Contribute to the working anarchy we fondly call the Internet
|
Posted By: Diep-Vriezer
Date Posted: 26 November 2003 at 11:49am
|
Thanks! That little piece of code changed the way I handle XML! Using datasets never really occurred to me..
Anyway, I have the following situation (New code always causes new problems)
<? xml version="1.0" encoding="utf-8" ?>
< Configuration>
<Cryptography>
<Key1> 710398740561298347056981239847105601928347019856029384719856 012934098</Key1>
<Key2> 091098703948701928709870981230986501928370651982706519283470 615347913</Key2>
</Cryptography>
</Configuration>
|
Now I want to retrieve the keys, so I made the following line of code:
objDataSet.Tables.Item("Cryptography").Columns("Key1").ToStr ing
|
This returns 'Key1', the name of the collum. How can I get the XML inner text, or the value? I've tried quite a few things, including the container.components, but it just won't work. Any ideas?
------------- Gone..
|
Posted By: Mart
Date Posted: 26 November 2003 at 12:33pm
Try
objDataSet.Tables("Cryptography").Rows(0).Items("Key1")
(you need to increment 0 row for the row heres how to loop through)
Dim RowCount As Integer = objDataSet.Tables("Cryptography").Rows.Count
Dim i As Integer
For i = 0 To RowCount - 1
Dim value As String objDataSet.Tables("Cryptography").Rows(i).Items("Key1")
...
Next
|
Posted By: Diep-Vriezer
Date Posted: 26 November 2003 at 12:39pm
|
Fantastic. I forgot about the .Rows(0).. Anyway, thanks, now I can also update it the same way (little modified, but same idea)!
------------- Gone..
|
Posted By: Diep-Vriezer
Date Posted: 26 November 2003 at 12:47pm
This is great, the code I made requires 3 lines, and takes away all the XML node stuff. Really 
------------- Gone..
|
Posted By: MorningZ
Date Posted: 26 November 2003 at 12:59pm
|
yeah, open that above xml file you posted in Visual Studio.NET
then click the "Data" tab on the bottom..... hey look! its a DataSet 
------------- Contribute to the working anarchy we fondly call the Internet
|
Posted By: Mart
Date Posted: 26 November 2003 at 1:00pm
|
Yep i was amazed when i first used it, it was a lot quicker and easier than MSSQL and you can do the same stuff.
|
Posted By: Diep-Vriezer
Date Posted: 26 November 2003 at 1:22pm
Do you think it's smarter to use Acces or XML for a login system? I know Acces has limited connections, and I was wondering if XML isn't a better solution.
------------- Gone..
|
Posted By: Mart
Date Posted: 26 November 2003 at 1:33pm
|
I have recently made a auth system in XML. Its very stable and a lot
faster. The admin panel is also great. I would prefer to use XML over
access because access is pants. If i do need a proper database solution
i woud use SQL server if possible.
|
Posted By: MorningZ
Date Posted: 26 November 2003 at 1:37pm
|
the article nails it right in the head in the second paragraph:
"Sometimes, though, for smaller Web sites or for small amounts of data, a database seems like a bit of overkill"
if its a small site, a few logins, then an XML document should be fine (in fact, you can stash some username/password combos in the security areas of config.web)
XML is not by any means a replacement for a database, if the sites gets busy, lots or big XML files will be very hard to manage
"Access has limited connections".. that's a very common misconception.. Access has limited concurrent connections (meaning hitting the site at the same exact time, just because there might be 200 people on the site doesn't mean there's 200 simulatanious hits on the database), compared to SQL server.. not compared to just anything.....
------------- Contribute to the working anarchy we fondly call the Internet
|
Posted By: Mart
Date Posted: 26 November 2003 at 1:47pm
|
Also with access if you are smart with your connections you can milk some more...
|
Posted By: Diep-Vriezer
Date Posted: 26 November 2003 at 2:54pm
|
Mmm, I guess I'll take SQL, AND XML . XML for auth, and SQL for the rest of the site (lots of DB involved).
EDIT: Exactly the other way. XML for the rest of the site (.config files) and SQL/Acces for auth.
------------- Gone..
|
Posted By: Diep-Vriezer
Date Posted: 28 November 2003 at 8:52am
|
Help! Got a new error.. What's wrong with the following code?
Dim objDataSet As New DataSet, objDatarow As DataRow
objDataSet.ReadXml(HttpContext.Current.Server.MapPath("/Conf iguration/Authorization/Users.config"))
objDatarow.Item("AccountTest") = "Boe!"
objDatarow.Item("AccountTest2") = "Boe!^2"
objDataSet.Tables.Add("Account.TestEntry")
objDataSet.Tables("Account.TestEntry").Columns.Add("AccountT est")
objDataSet.Tables("Account.TestEntry").Columns.Add("AccountT est2")
objDataSet.Tables("Account.TestEntry").Rows.Add(objDatarow)
objDataSet.WriteXml(HttpContext.Current.Server.MapPath("/Con figuration/Authorization/Users.config"))
|
I get the 'Object reference not set to an instance of an object' error on line 30, where I attemp to add some variables to the datarow. I've tried
Dim objDatarow As New Datarow()
But that won't work either. What's wrong?
------------- Gone..
|
Posted By: Mart
Date Posted: 28 November 2003 at 9:23am
Try objDatarow As New DataRow. But if not dont use a datarow, i didnt. Instead use a array according to the columns index i.e.
Dim Data(1)
Data(0) = "Boe!"
Data(1) = "Boe!^2"
... Add the columns to the table.
objDataSet.Tables("Account.TestEntry").Rows.Add(Data)
|
Posted By: Diep-Vriezer
Date Posted: 28 November 2003 at 10:58am
|
This works alright, by the way, thanks! Quite weird, I tried this before.
EDIT, The New DataRow doesn't work, it tells me this:
'System.Data.Datarow.Protected Sub New (builder As System.Data.Datarowbuilder)' is not accessible in this content because it is 'Protected'
What should I do now?
------------- Gone..
|
Posted By: Mart
Date Posted: 28 November 2003 at 11:23am
|
Remove the new, did you try what i said if it doesnt work?
|
Posted By: Diep-Vriezer
Date Posted: 28 November 2003 at 11:31am
|
Mart wrote:
Try objDatarow As New DataRow. But if not dont use a datarow, i didnt. Instead use a array according to the columns index i.e.
|
You said I had to try the new.. Anyway, the array works, but I was wondering how to use the datarow, since the collum index changes a lot, and with datarow you can tell what the content is with the name of the collumn.
------------- Gone..
|
Posted By: Mart
Date Posted: 28 November 2003 at 11:39am
Try this:
Dim objDataRow As DataRow
objDataRow =
ds.Tables("customers").NewRow()
|
|
Posted By: Diep-Vriezer
Date Posted: 28 November 2003 at 11:48am
Aaah, that might be it, thanks!
------------- Gone..
|
Posted By: Mart
Date Posted: 29 November 2003 at 12:51pm
|
How can you execute an Xpath query on a dataset?
|
Posted By: Diep-Vriezer
Date Posted: 29 November 2003 at 5:05pm
|
Xpath? Have you tried:
Dim dataset As New Dataset
Dataset.ReadXML("Blala.xml") Dataset.Select("YourQuery", xx)
------------- Gone..
|
Posted By: Mart
Date Posted: 30 November 2003 at 3:08am
|
Is 'YourQuery' an xpath query. P.s Xpath is a kind of XML version of SQL
|
Posted By: Diep-Vriezer
Date Posted: 30 November 2003 at 6:30am
Yeah, I guess so, try it, and you'll know. I'll look up some tutorials on Xpath.
------------- Gone..
|
Posted By: Mart
Date Posted: 30 November 2003 at 9:26am
It isnt for Xpath expressions I just looked in VS.NET. I can't remember exactly what it said because its sunday and im nakered
|
Posted By: Diep-Vriezer
Date Posted: 30 November 2003 at 10:04am
Haha, I'll try to find it to.
------------- Gone..
|
Posted By: Diep-Vriezer
Date Posted: 30 November 2003 at 10:42am
|
... (error)
What am I doing wrong with this one? The .Select() should return a datarow, but it won't accept it.
Dim objDataSet As New DataSet, objDataRow As DataRow
objDataSet.ReadXml(HttpContext.Current.Server.MapPath("/Conf iguration/Account.config"))
objDataRow = objDataSet.Tables("Account").Select("AccountName = '" & "Test" & "' And AccountPassword = '" & "Bah" & "'")
|
------------- Gone..
|
Posted By: Mart
Date Posted: 30 November 2003 at 10:51am
If you have a look on the intellisense on the select method:
Gets an array of all System.Data.DataRow objects.
So it doesnt execute an Xpath query But theres gotta be a way otherwise there wouldnt be a System.Xml.XPath namespace.
I was thinking load up an XML document, do the Xpath expression, Load
it into a System.Xml.XmlTextReader, then load it into a System.DataSet.
But how would you do that?
|
Posted By: Diep-Vriezer
Date Posted: 01 December 2003 at 10:23am
I really don't know.. I can't find anything really usefull about Xpath expressions btw, I only find articles about the Xpath navigator =/
------------- Gone..
|
Posted By: Mart
Date Posted: 01 December 2003 at 11:02am
me too < ="">
>
|
Posted By: MorningZ
Date Posted: 01 December 2003 at 5:03pm
http://www.w3schools.com/xpath/default.asp - a good start
------------- Contribute to the working anarchy we fondly call the Internet
|
Posted By: Diep-Vriezer
Date Posted: 01 December 2003 at 11:52pm
|
... is half the work. Thx 
------------- Gone..
|
Posted By: Mart
Date Posted: 07 December 2003 at 6:02am
|
private const localURL as String = " http://localhost/quickstart/howto/samples/Xml/QueryXmldocumentXPath/cs/books.xml - http://localhost/quickstart/howto/samples/Xml/QueryXmlDocume ntXPath/cs/books.xml "
Shared sub Main Dim myQueryXmlDocumentXPathSample as QueryXmlDocumentXPathSample myQueryXmlDocumentXPathSample = new QueryXmlDocumentXPathSample() myQueryXmlDocumentXPathSample.Run(localURL) end sub
public sub Run(args as String) Console.WriteLine("XPath Test started ...")
Dim myXPathDocument as XPathDocument = new XPathDocument(args) Dim myXPathNavigator as XPathNavigator = myXPathDocument.CreateNavigator()
' Get all the book prices XPathQuery(myXPathNavigator, "descendant::book/price")
' Get the ISBN of the last book XPathQuery(myXPathNavigator, "bookstore/book[3]/@ISBN") end sub
private sub XPathQuery(myXPathNavigator as XPathNavigator, xpathexpr as String )
try Console.WriteLine("XPath query: " + xpathexpr)
' Create a node interator to select nodes and move through them (read-only) Dim myXPathNodeIterator as XPathNodeIterator = myXPathNavigator.Select (xpathexpr)
while (myXPathNodeIterator.MoveNext()) Console.WriteLine("<" & myXPathNodeIterator.Current.Name + "> " & myXPathNodeIterator.Current.Value) end while
Console.WriteLine() catch e as Exception Console.WriteLine ("Exception: {0}", e.ToString()) end try end sub
|
Posted By: Diep-Vriezer
Date Posted: 07 December 2003 at 7:34am
Nice! This'll do the job!
------------- Gone..
|
|