XML Reading in VB.net

B

BiT

Hello

i'm tryin` to read xml elements from NZB type file (Link to nzb sample:
http://en.wikipedia.org/wiki/Nzb)

I worte this code but it dosen't find any elements (I need to get the
message ID)
Sub NZBToMsgID(ByVal _FileLnk As String, ByVal msgIDbox As ComboBox)

Dim m_xmld As XmlDocument

Dim m_nodelist As XmlNodeList

Dim m_node As XmlNode

'Create the XML Document

m_xmld = New XmlDocument()

'Load the Xml file

m_xmld.Load(_FileLnk)

'Get the list of name nodes

m_nodelist = m_xmld.SelectNodes("/nzb/file/segments/segment")

'Loop through the nodes

For Each m_node In m_nodelist

msgIDBox.Items.Add(m_node.ChildNodes.Item(0).InnerText)

Next

End Sub


thanks
 
M

Martin Honnen

BiT said:
i'm tryin` to read xml elements from NZB type file (Link to nzb sample:
http://en.wikipedia.org/wiki/Nzb)

I worte this code but it dosen't find any elements (I need to get the
message ID)
Sub NZBToMsgID(ByVal _FileLnk As String, ByVal msgIDbox As ComboBox)

Dim m_xmld As XmlDocument

Dim m_nodelist As XmlNodeList

Dim m_node As XmlNode

'Create the XML Document

m_xmld = New XmlDocument()

'Load the Xml file

m_xmld.Load(_FileLnk)

'Get the list of name nodes

m_nodelist = m_xmld.SelectNodes("/nzb/file/segments/segment")

You need to bind a prefix to the default namespace e.g.
Dim namespaceManager As XmlNamespaceManager = New
XmlNamespaceManager(m_xmld.NameTable)
namespaceManager.AddNamespace("pf",
"http://www.newzbin.com/DTD/2003/nzb")
m_nodelist = m_xmld.SelectNodes("/pf:nzb/pf:file/pf:segments/pf:segment")
 
B

BiT

i've tried it but i get error message:
"Namespace Manager or XsltContext needed. This query has a prefix, variable,
or user-defined function."

here's the code:

Sub NZBToMsgID(ByVal _FileLnk As String)
Dim m_xmld As XmlDocument

Dim m_nodelist As XmlNodeList

Dim m_node As XmlNode

'Create the XML Document

m_xmld = New XmlDocument()

'Load the Xml file

m_xmld.Load(_FileLnk)

Dim namespaceManager As XmlNamespaceManager = New
XmlNamespaceManager(m_xmld.NameTable)

namespaceManager.AddNamespace("pf", "http://www.newzbin.com/DTD/2003/nzb")

m_nodelist = m_xmld.SelectNodes("/pf:nzb/pf:file/pf:segments/pf:segment")

'Loop through the nodes

For Each m_node In m_nodelist

Dim a = m_node.ChildNodes.Item(0).InnerText

Next

End Sub
 
M

Martin Honnen

BiT said:
i've tried it but i get error message:
"Namespace Manager or XsltContext needed. This query has a prefix,
variable, or user-defined function."

My bad, forgot to change
m_nodelist = m_xmld.SelectNodes("/pf:nzb/pf:file/pf:segments/pf:segment")

to
m_nodelist =
m_xmld.SelectNodes("/pf:nzb/pf:file/pf:segments/pf:segment",
namespaceManager)
 
B

BiT

ok it's work thank you.

i tried to play with it and understand how this namespace work, but i didn't
=>
can u explain little on this namespace and for example how can i retrive the
segment size or the file name

thank you again
 
B

BiT

i tried use the XMLtextreader but still i don't understand how to navigate
in such xml file with namesapces

thanks
 
M

Martin Honnen

BiT said:
i tried to play with it and understand how this namespace work, but i
didn't =>
can u explain little on this namespace and for example how can i retrive
the segment size or the file name

If you have an XML document starting like this:

<nzb xmlns="http://www.newzbin.com/DTD/2003/nzb">
<file subject="Linux Debian-3.1r5-i386-binary- CD1 Lucky Strike.par2
(1/1)"
date="1175058765" poster="(e-mail address removed) (Lucky Strike)">
<groups>
<group>alt.binaries.cd.image.linux</group>
</groups>
<segments>
<segment bytes="423495"
number="1">[email protected]</segment>

then the root element nzb has a default namespace declaration
xmlns="http://www.newzbin.com/DTD/2003/nzb"
which applies to the root element and all its descendant elements
(unless a descendant had it own redeclaration of xmlns).
So this means that the nzb element and its descendant elements like
file, groups, group, segments, segment are in the namespace with name
http://www.newzbin.com/DTD/2003/nzb.

For XPath 1.0 to match such elements you need to bind a prefix to the
namespace URI and use that prefix in XPath expressions. That is
necessary as in an XPath expression
nzb
always matches nzb elements in _no_ namespace.

As for accessing a segment size in bytes, that is an attribute of the
element you can access like this:

For Each segment As XmlElement In
xmlDoc.SelectNodes("/pf:nzb/pf:file/pf:segments/pf:segment",
namespaceManager)
Console.WriteLine(segment.Attributes("bytes").Value)
Next
 

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

Similar Threads


Top