2014-07-24 2 views
0

xml 노드를 업데이트하고 사이트를 검색하여 this link에서 예제를 찾으십시오. 그러나 개체 참조에 대한 오류가 개체의 인스턴스로 설정되지 않습니다. 누군가 노드를 가져 오는 방법을 보여 주겠습니까? 당신은 Order 가진 첫번째 order을 닫습니다 - 모든특정 XML 노드를 가져올 수 없습니다.

<?xml version="1.0" encoding="utf-8"?> 
    <Orders> 
    <order ID="2"> 
     <item>Organ</item> 
     <Date>7/24/2014 3:50:42 PM</Date> 
     <Country>China</Country> 
    </Order> 
    <order ID="1"> 
    <item>Apple</item> 
    <Date>7/24/2014 3:50:42 PM</Date> 
    <Country>China</Country> 
    </order> 
</Orders> 
+0

어떤 줄 올리기 오류가 있습니까? – Fabio

+0

오류가 발생한 행을 정교하게 설명 할 수 있습니까? 첫 번째 모습에서, 나는'xmlFilePath'가 잘못 작성된 '& xmlFileNamae'가 아니라고 생각합니다.'.Load()'가 오류를 발생시킵니다. –

+0

@Nadeem_MK,이 줄에 Dim 노드로 XmlNode = docXML.SelectSingleNode ("/ Files/File [@ ID = '"& ID & "']/Date")를 던집니다. 노드가 없습니다. 노드가 없으므로 – user819774

답변

0

먼저 XML이 분류됩니다 내 xml 파일

Imports System.Xml 
Imports System.IO 

Partial Class test2 
Inherits System.Web.UI.Page 


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  

    Dim xmlFileNamae As String = "Vancouver.xml" 
    Dim xmlFilePath As String = ConfigurationManager.AppSettings("XMLFolder") & xmlFileNamae 
    If File.Exists(xmlFilePath) Then 
     Dim docXML As XmlDocument = New XmlDocument 
     docXML.Load(xmlFilePath) 
     Dim ID As String = "1" 
     Dim node As XmlNode = docXML.SelectSingleNode("/Orders/Order[@ID='" & ID & "']/Date") 
     node.InnerText = Date.Now 
     node = docXML.SelectSingleNode("/Orders/Order[@ID='" & ID & "']/Country") 
     node.InnerText = "Vancouver" 
     docXML.Save(xmlFilePath) 
    End If 
End Sub 

End Class 

있습니다 : 사전

감사이 내 VB 코드 그것은 틀렸다.

두 번째로 XPath가 좋지 않습니다.

"깨끗한"코드가있는 두 XPath가 있습니다.

Dim XmlDoc As New XmlDocument 
    Dim Node As XmlNode 
    XmlDoc.Load(//Your path) 
    //This will give you the date of the first order item in orders 
    //where the ID attribute equals 1. 
    Node = XmlDoc.SelectSingleNode("/Orders/order[@ID="1"]/Date") 
    //Do somehintg 
    //This will give you the country of the first order item in orders 
    //where the ID attribute equals 1. 
    Node=XmlDoc.SelectSingleNode("/Orders/order[@ID="1"]/Country") 
    //Do something else.