Get the Number of line in XML doc using vb.net and xElement

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I need to retrieve the line and col number of an xml document when I m parsing it on vb.net; here is an example:

xml:

<?xml version = "1,0"encoding = "utf-8"?>        <!-- line 1 -->

 <user>                                          <!-- line 2 -->
        <name>dave</name>                        <!-- line 3 -->
        <age>29</age>                            <!-- line 4 -->
        <function>medic</function>               <!-- line 5 --><!-- col 8  -->
 </user>                                         <!-- line 6 -->

vb:

Dim xelement As XElement = xelement.load(xmlDocName) 
 xmlDocName is the path of the document 

Dim nodes = from data in xelement.elements("user")
for each xEle in nodes
        name = xEle.Elements.("name").value.toString
        age  = xEle.Elements.("age").value.toString
        func = xEle.Elements.("function").value.toString

        if func = string.empty then
         Here is my problem: 
         I need some thing like that: 
            line = xEle.Elements.("age").lineNumber.toString
             the variable line should receve the value  5  
             col = xEle.Elements.("age").colNumber.toString
             the variable col should receve the value  8  

           But those methods does not exist... 
        end if
next

I need an help using the XElement class, if it is not possible I will have to change it.

Answers

I Found a way to get the line number, it is working but I do not know if it is the best way to solve my problem. Also I have to test the performace for using this code in big documents and to process a great deal of xmls.

vb:

Dim xelement As XElement = xelement.load(xmlDocName,LoadOptions.SetLineInfo) 
 xmlDocName is the path of the document   
Dim nodes = from data in  xelement.elements("user")
for each xEle in nodes
      name = xEle.Elements.("name").value.toString
      age  = xEle.Elements.("age").value.toString
      func = xEle.Elements.("function").value.toString

      if func = string.empty then
          Dim nodes2 = from data in xelement.elements("user").elements("function")
          Dim info As IXmlLineInfo = nodes2.first
          line = info.LineNumber
      end if
next

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/25041112/get-the-number-of-line-in-xml-doc-using-vb-net-and-xelement

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils