Reading XML Documents

D

David Lozzi

Howdy,

I have an XML document, see below. I'm trying to read from it and pull
attributes and elements as needed. The only thing i've been able to get to
work is using the Read method of the XmlReader class. I see there are other
methods of moving around but none appear to be working properly. Could you
suggest a better approach following the below flow?

<primarysite Title="Reading Deparment" Description="" Theme=""
ServerRelativeUrl="/">
<users>
<user Name="ROCK\administrator" LoginName="ROCK\administrator" Email="">
<roles />
</user>
<user Name="System Account" LoginName="SHAREPOINT\system" Email="">
<roles />
</user>
</users>



Dim xConfig As New XmlTextReader("c:\wss\Reading_Deparment.xml") '"
& PrimarySiteFile & ".xml")

With xConfig
.MoveToContent()
TimeEntry("Title=" & .GetAttribute("Title") & " Description=" &
..GetAttribute("Description"))
.Read() 'users
.Read() 'user
Do While .Name = "user"
TimeEntry("LoginName=" & .GetAttribute("LoginName") & "
Email=" & .GetAttribute("Email") & " Name=" & .GetAttribute("Name"))
.Read() ' roles
.Read() ' user or role
If .Name = "role" Then
'process the role
End If
.Read() ' user
Loop
.ReadEndElement() ' users

Thanks!
 
G

Guest

Howdy,

I have an XML document, see below. I'm trying to read from it and pull
attributes and elements as needed. The only thing i've been able to get to
work is using the Read method of the XmlReader class. I see there are other
methods of moving around but none appear to be working properly. Could you
suggest a better approach following the below flow?

<primarysite Title="Reading Deparment" Description="" Theme=""
ServerRelativeUrl="/">
<users>
<user Name="ROCK\administrator" LoginName="ROCK\administrator" Email="">
<roles />
</user>
<user Name="System Account" LoginName="SHAREPOINT\system" Email="">
<roles />
</user>
</users>

Dim xConfig As New XmlTextReader("c:\wss\Reading_Deparment.xml") '"
& PrimarySiteFile & ".xml")

With xConfig
.MoveToContent()
TimeEntry("Title=" & .GetAttribute("Title") & " Description=" &
.GetAttribute("Description"))
.Read() 'users
.Read() 'user
Do While .Name = "user"
TimeEntry("LoginName=" & .GetAttribute("LoginName") & "
Email=" & .GetAttribute("Email") & " Name=" & .GetAttribute("Name"))
.Read() ' roles
.Read() ' user or role
If .Name = "role" Then
'process the role
End If
.Read() ' user
Loop
.ReadEndElement() ' users

Thanks!

Hi David,

I think the easiest way is to use XmlDocument and XmlNodeList.

For example:

Dim xml as XmlDocument = New XmlDocument()
xml.Load(HttpContext.Current.Server.MapPath("...xml"))
Dim nodes as XmlNodeList = xml.SelectNodes("//users/user")
For each n as XmlNode In nodes
Response.Write(n.Attributes["Name"].Value)
Next

Hope this helps
 
D

David Lozzi

That's doing basically the same thing. I could loop through the .Read but the
script gets messy at best. The method i have now i can at least know where in
the script each group of nodes are being processed.

I've seen methods like ReadStartElement("users") which I tried but it threw
an error

..MoveToContent()
TimeEntry("Title=" & .GetAttribute("Title") & " Description=" &
..GetAttribute("Description"))
..ReadStartElement("users") **LINE 215
..Read() 'user

System.Xml.XmlException: Element 'users' was not found. Line 1, position 192.
at System.Xml.XmlReader.ReadStartElement(String name)
at SharePointExportImport.Form1.btnImport_Click(Object sender, EventArgs
e) in C:\Documents and Settings\Administrator\My Documents\Visual Studio
2005\Projects\SharePointExportImport\SharePointExportImport\Form1.vb:line 215

Thanks!

--
David Lozzi
Delphi Technology Solutions
Blog: www.lozzi.net


Anon User said:
Howdy,

I have an XML document, see below. I'm trying to read from it and pull
attributes and elements as needed. The only thing i've been able to get to
work is using the Read method of the XmlReader class. I see there are other
methods of moving around but none appear to be working properly. Could you
suggest a better approach following the below flow?

<primarysite Title="Reading Deparment" Description="" Theme=""
ServerRelativeUrl="/">
<users>
<user Name="ROCK\administrator" LoginName="ROCK\administrator" Email="">
<roles />
</user>
<user Name="System Account" LoginName="SHAREPOINT\system" Email="">
<roles />
</user>
</users>

Dim xConfig As New XmlTextReader("c:\wss\Reading_Deparment.xml") '"
& PrimarySiteFile & ".xml")

With xConfig
.MoveToContent()
TimeEntry("Title=" & .GetAttribute("Title") & " Description=" &
.GetAttribute("Description"))
.Read() 'users
.Read() 'user
Do While .Name = "user"
TimeEntry("LoginName=" & .GetAttribute("LoginName") & "
Email=" & .GetAttribute("Email") & " Name=" & .GetAttribute("Name"))
.Read() ' roles
.Read() ' user or role
If .Name = "role" Then
'process the role
End If
.Read() ' user
Loop
.ReadEndElement() ' users

Thanks!

Hi David,

I think the easiest way is to use XmlDocument and XmlNodeList.

For example:

Dim xml as XmlDocument = New XmlDocument()
xml.Load(HttpContext.Current.Server.MapPath("...xml"))
Dim nodes as XmlNodeList = xml.SelectNodes("//users/user")
For each n as XmlNode In nodes
Response.Write(n.Attributes["Name"].Value)
Next

Hope this helps
 
G

Guest

That's doing basically the same thing.

Yup. With less code :)

I could loop through the .Read but the
script gets messy at best. The method i have now i can at least know where in
the script each group of nodes are being processed.

I've seen methods like ReadStartElement("users") which I tried but it threw
an error

.MoveToContent()
TimeEntry("Title=" & .GetAttribute("Title") & " Description=" &
.GetAttribute("Description"))
.ReadStartElement("users") **LINE 215
.Read() 'user

Your start element is <primarysite> and that's the reason of the
error.

More about ReadStartElement
http://msdn2.microsoft.com/en-us/library/y7e4769a.aspx
 
G

Guest

um.... and what should it be? Why is that the error?

Because it cannot find the start element named "users" at the current
position. Either add .ReadStartElement("primarysite")
before .ReadStartElement("users"), or do the loop

With xConfig
Do While .Read
If .IsStartElement And .Name = "primarysite" Then
Dim Title As String = .GetAttribute("Title")
....
End If
Loop
End With

or

Do While xConfig.Read()
If (xConfig.Name.Equals("user")) Then
Dim LoginName As String = xConfig.GetAttribute("LoginName")
....
End If
Loop

Hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top