C# 如何从xml文件中读取单个节点值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18250671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:40:23  来源:igfitidea点击:

How to read single node value from xml file

c#asp.netxmlvb.net

提问by deepika

Hi i am trying to get value from xml but it shows node null.

嗨,我正在尝试从 xml 获取值,但它显示节点为空。

Here is my xml file.

这是我的 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>

I want to get "MailingGUID"value.

我想获得“MailingGUID”值。

Here is the code that i have tried:

这是我尝试过的代码:

  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;


    }

Please tell me how i can get MailingGUIDvalue.

请告诉我如何获得MailingGUID值。

Thanks

谢谢

回答by Marcus

UPDATE:I think there might be something wrong with your schemas, I removed references to them and your code worked fine. I tried this:

更新:我认为您的架构可能有问题,我删除了对它们的引用并且您的代码运行良好。我试过这个:

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

回答by user3387482

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());
        }