Re: Reading XML Documents

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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


"Alexey Smirnov" wrote:

On Nov 19, 9:41 pm, David Lozzi <dlozzi(remove-this)@delphi-ts.com>
wrote:
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!

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

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

.



Relevant Pages

  • Re: Reading XML Documents
    ... I have an XML document, ... I'm trying to read from it and pull ... I think the easiest way is to use XmlDocument and XmlNodeList. ... For each n as XmlNode In nodes ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Adding attributes to an element
    ... XmlDocument xmlDoc = new XmlDocument; ... XmlNode xmlNode; ... > I have an XML document and I need to add some attributes to an element. ...
    (microsoft.public.dotnet.xml)
  • RE: inserting XML Document into Oracle database using a stored procedure
    ... error message. ... I expect the script to insert a xml document into the oracle database. ...
    (perl.dbi.users)
  • Re: XML document causes pickle to go into infinite recursion
    ... and caches this data using shelveleto minimize load on the ... In general, the script works quite well, but keeps crashing ... Someone on Python-Tutor suggested that the XML document has ... a circular reference, but I'm not sure exactly what this means, or why ...
    (comp.lang.python)
  • RE: reordering elements before processing
    ... "Jim M" wrote: ... I think it's interesting that my script worked in the ... > mapper for you. ... >> What do you mean when say that "the elements in the xml document are ...
    (microsoft.public.biztalk.general)