Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中获取部分XML值#_C#_.net_Xml - Fatal编程技术网

C# 在C中获取部分XML值#

C# 在C中获取部分XML值#,c#,.net,xml,C#,.net,Xml,我有如下XML: <RES> <MUL> <SIN> <KEY name="a"> <VALUE>a1</VALUE> </KEY> <KEY name="b"> <VALUE>b1</VALUE> </KEY> <KEY name="c"> <VALUE>c1</VALUE&

我有如下XML:

<RES>
 <MUL>
  <SIN>
   <KEY name="a">
    <VALUE>a1</VALUE>
   </KEY>
   <KEY name="b">
    <VALUE>b1</VALUE>
   </KEY>
   <KEY name="c">
    <VALUE>c1</VALUE>
   </KEY>
   <KEY name="need">
    <MUL>
     <SIN>
      <KEY name="needID">
       <VALUE>ID</VALUE>
      </KEY>
      <KEY name="needOther">
       <VALUE>other</VALUE>
      </KEY>
      <KEY name="needOther2">
       <VALUE>other2</VALUE>
      </KEY>
     </SIN>
    </MUL>
   </KEY>
  </SIN>
 </MUL>
</RES>
但从那以后我就不能和needID打交道了

XDocument doc = new XDocument(node);
var cource = from x in doc.Descendants("KEY")
select new { ID = doc.Element("VALUE").Value };
请帮帮我


谢谢!:)

下面这样的怎么样

XDocument doc = XDocument.Load("url");

var cource = from x in doc.Descendants("KEY")
                 where x.Attribute("name").Value == "needID" 
                 select new { ID = x.Element("VALUE").Value };
谢谢


Deepu

您需要这样的东西:

// you're expecting only a single node - right?? So use .SelectSingleNode!
XmlNode node = xx.SelectSingleNode("/RES/MUL/SIN/KEY[@name='need']");

// if we found the node...
if(node != null)
{
    // get "subnode" inside that node
    XmlNode valueNode = node.SelectSingleNode("MUL/SIN/KEY[@name='needID']/VALUE");

    // if we found the <MUL>/<SIN>/<KEY name='needID'>/<VALUE> subnode....
    if(valueNode != null)
    {
        // get the inner text = the text of the XML element...
        string value = valueNode.InnerText;
    }
}

我会考虑使用XBelf和DealEnthStand()来接收异常:“对象引用不被设置为对象的实例”……上面粘贴的代码是一个。我不知道为什么它不适合你。如果将URL更改为XML文件路径。。它应该会起作用。或者,如果您将其作为字符串XDocument doc=XDocument.Parse(MYDOC),谢谢!我会尝试你的第一个解决方案,它对我有效!
// you're expecting only a single node - right?? So use .SelectSingleNode!
XmlNode node = xx.SelectSingleNode("/RES/MUL/SIN/KEY[@name='need']");

// if we found the node...
if(node != null)
{
    // get "subnode" inside that node
    XmlNode valueNode = node.SelectSingleNode("MUL/SIN/KEY[@name='needID']/VALUE");

    // if we found the <MUL>/<SIN>/<KEY name='needID'>/<VALUE> subnode....
    if(valueNode != null)
    {
        // get the inner text = the text of the XML element...
        string value = valueNode.InnerText;
    }
}
// define XPath
string xpath = "/RES/MUL/SIN/KEY[@name='need']/MUL/SIN/KEY[@name='needID']/VALUE";

// you're expecting only a single node - right?? So use .SelectSingleNode!
XmlNode node = xx.SelectSingleNode(xpath);

// if we found the node...
if(node != null)
{
    // get the inner text = the text of the XML element...
    string value = node.InnerText;
}
XmlDocument xml = new XmlDocument();

xml.Load(File.OpenRead(@"Your XML File"));

//XmlNodeList xnList = xml.SelectNodes("/RES/MUL/SIN/KEY");

//You can use something like the below if the XML file is large and you need to read in more than one
//foreach (XmlNode xn in xnList)
//{
//Have a seperate class to store the values
//class class = new class();
//class.ID = xn.SelectSingleNode("./@needID").Value;
//
//}