2013-08-15 33 views
10

こんにちは、私はXMLから値を取得しようとしていますが、ノードヌルを示しています。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"値を取得したいです。

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; 


    } 

を私はMailingGUID値を得ることができる方法を教えてください:ここでは

は私がしようとしているコードです。

おかげ

+0

、問題はあなたのXPathクエリである:私はこれを試してみました。ここをクリックしてください:[リンク](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ノード= xml.DocumentElement.SelectSingleNode( "/ result/data"); //その値を取得 string name = node.InnerText;これは私にオブジェクトの参照のエラーを与える.. – 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)は、ノードにxxxタグ – thepen

+0

が入っている私のURLパスをロードしています。存在するのはなので、LabelESというラベルにnumtoを渡します。 – 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()); 
     } 
関連する問題