2013-08-15 3 views
10

안녕하세요, XML에서 가치를 얻으려고하고 있지만 노드가 null 인 것으로 나타납니다.XML 파일에서 단일 노드 값을 읽는 방법

다음은 내 xml 파일입니다.

<?xml version="1.0" encoding="utf-8"?> 
<result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.cfhdocmail.com/TestAPI2/Result.xsd https://www.cfhdocmail.com/TestAPI2/Result.xsd" xmlns="https://www.cfhdocmail.com/TestAPI2/Result.xsd"> 
    <data> 
    <key>MailingGUID</key> 
    <value>0aa2b2e3-7afa-4002-ab2f-9eb4cbe33ae7</value> 
    </data> 
    <data> 
    <key>OrderRef</key> 
    <value>52186</value> 
    </data> 
</result> 

나는 "MailingGUID" 값을 얻을합니다. 내가 값을 MailingGUID 얻을 수있는 방법

private void readXML() 
    { 
     XmlDocument xml = new XmlDocument(); 
     // You'll need to put the correct path to your xml file here 
     xml.Load(Server.MapPath("~/XmlFile11.xml")); 

     // Select a specific node 
     XmlNode node = xml.SelectSingleNode("result/data/value"); 
     // Get its value 
     string name = node.InnerText; 


    } 

을 가르쳐주세요 : 여기

내가 시도 코드입니다.

감사

+0

문제가 XPath 쿼리 함께 : 나는 이것을 시도했다. 여기를보십시오 : [Link] (http://www.w3schools.com/xpath/) – Artless

답변

9

UPDATE : 나는 당신의 스키마에 문제가, 내가 그들에 대한 참조를 제거하고 코드가 잘 작동있을 수 있습니다 생각합니다.

당신이 올바른 궤도에있어
const string str = "<?xml version=\"1.0\" encoding=\"utf-8\"?><result><data><key>MailingGUID</key><value>0aa2b2e3-7afa-4002-ab2f-9eb4cbe33ae7</value></data><data><key>OrderRef</key><value>52186</value></data></result>"; 
var xml = new XmlDocument(); 
xml.LoadXml(str); 
xml.DocumentElement.SelectSingleNode("/result/data/value").InnerText 
+1

XmlNode node = xml.DocumentElement.SelectSingleNode ("/ result/data"); // 해당 값 가져 오기 string name = node.InnerText; 이 날 개체를 refrence의 오류를 제공합니다 .. – deepika

+0

내 업데이 트를 참조하십시오 .. – Marcus

+0

왜 내가 null을 얻고있다. 여기 코드입니다 : private void readXML() { XmlDocument xml = new XmlDocument(); xml.Load (Server.MapPath ("~/XMLFile1.xml")); var node = xml.DocumentElement.SelectSingleNode ("/ result/data"); var s = 노드 [ "값"]. InnerText; } – deepika

-1
xml.LoadXml(sResponse); 
XmlNodeList Nnodes = xml.SelectNodes("/api_result/entries_success"); 

foreach (XmlNode Nnode in Nnodes) 
{ 
    string entries_success = Nnode["numto"].InnerText; 
    LabelES.Text += entries_success + ","; 
} 
+0

코드에 대한 설명이 유용 할 것입니다. – Raidri

+0

(sResponse)은 노드에 – thepen

+0

xml 태그가 포함 된 URL 경로를로드하고 있는데이 노드는 이며 numto를 LabelES라는 레이블에 전달합니다. – thepen

0
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 


DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder builder = factory.newDocumentBuilder();     

     //Parsing of xml is done here 
     Document doc = builder.parse(new File("C:\\Users\\User_Name\\Documents\\My Received Files\\PDSL_ABM.xml")); 

     //Here we get the root element of XML and print out 
     doc.getDocumentElement().normalize(); 
     System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName()); 

     NodeList list = doc.getElementsByTagName("MailingGUID"); 
     int totalMailingGUID =list.getLength(); 
     System.out.println("Total no of MailingGUID : " + totalSupplierPartID); 



     //Traversing all the elements from the list and printing out its data 
     for (int i = 0; i < list.getLength(); i++) { 

     //Getting one node from the list. 
     Node childNode = list.item(i); 
     System.out.println("MailingGUID : " + childNode.getTextContent()); 
     } 
관련 문제