如何从当前节点读取xml文件的子节点

时间:2015-02-24 14:58:51

标签: xml vb.net

这是我的第一篇文章。因此,如果我做了任何不正当的事,请原谅我。

我想从当前节点读取所有子节点 。为了进一步解释,我创建了一个节点列表,并为每个节点确定以下内容: 01.查找当前节点有多少个子节点。 02.生成此类节点名称的列表。 03.对于一个节点,我有多个同名的子节点。如何在不总是获取第一个节点的值的情况下获取它们的值。 04.我附加了我的代码以及xml片段。 05.我想要的是在xml doc的第76行中的第52行和183336中提取-183336。 06. VB.Net要求的解决方案

环境 Windows 8,VS 2013 Express.Windows表单应用程序 很感激帮助。 最好的祝福 Varadarajan R

代码

Private Sub VCHLED_Click(sender As Object, e As EventArgs) Handles VCHLED.Click

    Me.Text = gApplicationTitle + gUsedDataBase

    '' cleanup the xml file to remove unwanted / complicating text

    gXMLFile = "VOUCHER1.XML"
    gXMLFilewithFullPath = gXMLFileLocation + gXMLFile

    gXMLTag = "VOUCHER"

    '' Handle xml file

    Dim xmlDoc As New XmlDocument()
    xmlDoc.Load(gXMLFilewithFullPath)
    Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("VOUCHER")
    Dim xelement As XElement = xelement.Load(gXMLFilewithFullPath)
    Dim root As XmlElement = xmlDoc.DocumentElement
    Dim elemList As XmlNodeList = root.GetElementsByTagName(gXMLTag)

    ' Determine how many nodes are there

    Dim NoOfNodes As Integer = elemList.Count()
    Dim NumberOfLoops As Integer = 1
    NumberOfLoops = Int(NoOfNodes / gMaxRowsForInsert) + 1


    '' Declare the Variables


    Dim mVchGuid As String
    Dim mLedgerName As String
    Dim mIsDeemedPositIve As String
    Dim mAmount As String

    Dim mMasterID As Integer
    Dim mAmountDr As Decimal
    Dim mAmountCr As Decimal


    Dim LoopsFinished As Integer = 0

    For Each node As XmlNode In nodes

        Dim NodeName As String = node.Name
        Debug.WriteLine(NodeName)
        If node.HasChildNodes Then
            Dim Children As Integer = node.ChildNodes.Count()
            Debug.WriteLine(" Child Nodes " + Children.ToString)
            Dim i As Integer = 0
            For i = 0 To Children - 1
                If node.SelectSingleNode("VOUCHER/ALLLEDGERENTRIES.LIST/AMOUNT") Is Nothing Then
                    mAmount = ""
                Else
                    mAmount = node.SelectSingleNode("VOUCHER/ALLLEDGERENTRIES.LIST/AMOUNT").InnerText
                    Debug.WriteLine(" Amount : " + mAmount)
                    Exit For
                End If
            Next
        End If
        Next
        End Sub

XML Fragment

<VOUCHER REMOTEID="1893406f-af1d-4d97-9b33-340f44694509-00002102" VCHKEY="1893406f-af1d-4d97-9b33-340f44694509-0000a303:00000010" VCHTYPE="Journal Voucher" ACTION="Create" OBJVIEW="Accounting Voucher View">
   <DATE>20140403</DATE>
   <GUID>1893406f-af1d-4d97-9b33-340f44694509-00002102</GUID>
   <NARRATION>Sra no.65 and 82 / 28.03.2014</NARRATION>
   <VOUCHERTYPENAME>Journal Voucher</VOUCHERTYPENAME>
   <VOUCHERNUMBER>JV/NF/00003/KM/14-15</VOUCHERNUMBER>
   <PARTYLEDGERNAME>Sri Lakshmi Fibre Coirs</PARTYLEDGERNAME>
   <PERSISTEDVIEW>Accounting Voucher View</PERSISTEDVIEW>
   <VCHGSTCLASS/>
   <ENTEREDBY>kkdsph</ENTEREDBY>
   <DIFFACTUALQTY>No</DIFFACTUALQTY>
   <AUDITED>No</AUDITED>
   <FORJOBCOSTING>No</FORJOBCOSTING>
   <ISOPTIONAL>No</ISOPTIONAL>
   <EFFECTIVEDATE>20140403</EFFECTIVEDATE>
   <ISFORJOBWORKIN>No</ISFORJOBWORKIN>
   <ALLOWCONSUMPTION>No</ALLOWCONSUMPTION>
   <USEFORINTEREST>No</USEFORINTEREST>
   <USEFORGAINLOSS>No</USEFORGAINLOSS>
   <USEFORGODOWNTRANSFER>No</USEFORGODOWNTRANSFER>
   <USEFORCOMPOUND>No</USEFORCOMPOUND>
   <ALTERID> 9852</ALTERID>
   <EXCISEOPENING>No</EXCISEOPENING>
   <USEFORFINALPRODUCTION>No</USEFORFINALPRODUCTION>
   <ISCANCELLED>No</ISCANCELLED>
   <HASCASHFLOW>No</HASCASHFLOW>
   <ISPOSTDATED>No</ISPOSTDATED>
   <USETRACKINGNUMBER>No</USETRACKINGNUMBER>
   <ISINVOICE>No</ISINVOICE>
   <MFGJOURNAL>No</MFGJOURNAL>
   <HASDISCOUNTS>No</HASDISCOUNTS>
   <ASPAYSLIP>No</ASPAYSLIP>
   <ISCOSTCENTRE>No</ISCOSTCENTRE>
   <ISSTXNONREALIZEDVCH>No</ISSTXNONREALIZEDVCH>
   <ISEXCISEMANUFACTURERON>No</ISEXCISEMANUFACTURERON>
   <ISBLANKCHEQUE>No</ISBLANKCHEQUE>
   <ISVOID>No</ISVOID>
   <ISONHOLD>No</ISONHOLD>
   <ISDELETED>No</ISDELETED>
   <ASORIGINAL>No</ASORIGINAL>
   <VCHISFROMSYNC>No</VCHISFROMSYNC>
   <MASTERID> 8450</MASTERID>
   <VOUCHERKEY>179233280229392</VOUCHERKEY>
   <ALLLEDGERENTRIES.LIST>
    <LEDGERNAME>Sri Lakshmi Fibre Coirs</LEDGERNAME>
    <GSTCLASS/>
    <ISDEEMEDPOSITIVE>Yes</ISDEEMEDPOSITIVE>
    <LEDGERFROMITEM>No</LEDGERFROMITEM>
    <REMOVEZEROENTRIES>No</REMOVEZEROENTRIES>
    <ISPARTYLEDGER>Yes</ISPARTYLEDGER>
    <ISLASTDEEMEDPOSITIVE>Yes</ISLASTDEEMEDPOSITIVE>
    <AMOUNT>-183336.00</AMOUNT>
    <BILLALLOCATIONS.LIST>
     <NAME>163</NAME>
     <BILLCREDITPERIOD>24-Mar-2014</BILLCREDITPERIOD>
     <BILLTYPE>Agst Ref</BILLTYPE>
     <TDSDEDUCTEEISSPECIALRATE>No</TDSDEDUCTEEISSPECIALRATE>
     <AMOUNT>-73353.00</AMOUNT>
     <INTERESTCOLLECTION.LIST>     </INTERESTCOLLECTION.LIST>
    </BILLALLOCATIONS.LIST>
    <BILLALLOCATIONS.LIST>
     <NAME>164</NAME>
     <BILLCREDITPERIOD>28-Mar-2014</BILLCREDITPERIOD>
     <BILLTYPE>Agst Ref</BILLTYPE>
     <TDSDEDUCTEEISSPECIALRATE>No</TDSDEDUCTEEISSPECIALRATE>
     <AMOUNT>-109983.00</AMOUNT>
   </ALLLEDGERENTRIES.LIST>
   <ALLLEDGERENTRIES.LIST>
    <LEDGERNAME>Company</LEDGERNAME>
    <GSTCLASS/>
    <ISDEEMEDPOSITIVE>No</ISDEEMEDPOSITIVE>
    <LEDGERFROMITEM>No</LEDGERFROMITEM>
    <REMOVEZEROENTRIES>No</REMOVEZEROENTRIES>
    <ISPARTYLEDGER>Yes</ISPARTYLEDGER>
    <ISLASTDEEMEDPOSITIVE>No</ISLASTDEEMEDPOSITIVE>
    <AMOUNT>183336.00</AMOUNT>
   </ALLLEDGERENTRIES.LIST>
     </VOUCHER>

0 个答案:

没有答案