Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.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# 如何检查XML值是否存在?_C#_Xml_Winforms - Fatal编程技术网

C# 如何检查XML值是否存在?

C# 如何检查XML值是否存在?,c#,xml,winforms,C#,Xml,Winforms,我试图从XML中获取数据。我所知道的是,当我试图找到的动物不在XML数据中时,我总是会出错。如图所示的示例 这是存储在XML中的数据: <?xml version="1.0" encoding="utf-8" ?> <Root> <Animal value="Elephant" size="2" name="Bob"> <Action age="1" size="1">I am small</Action> <Ac

我试图从XML中获取数据。我所知道的是,当我试图找到的动物不在XML数据中时,我总是会出错。如图所示的示例

这是存储在XML中的数据:

<?xml version="1.0" encoding="utf-8" ?>
<Root>
<Animal value="Elephant" size="2" name="Bob">
    <Action age="1" size="1">I am small</Action>
    <Action age="2" size="1">I am growing up</Action>
    <Action age="3" size="1">I'm 3 years old</Action>
    <Action age="4" size="1">I'm BIG</Action>
</Animal>
</Root>
错误发生在我更改时

string animal = "Tiger";

如果数据不存在,如何解决错误?

您可以使用
SelectNodes
作为
XmlNodeList
,然后检查其节点计数。如果计数为零,则表示“未找到节点”;否则,抓取属性并打印它:

XmlDocument xDoc = new XmlDocument();
xDoc.Load("animals.xml");
string animal = "Tiger";
XmlNodeList theList = xDoc.SelectNodes("/Root/Animal[@value='" + animal + "']");
if (theList.Count == 1) {
    MessageBox.Show(Convert.ToString(theList[0].Attributes["value"].InnerText));
} else if (theList.Count == 0) {
    MessageBox.Show("No "+animal);
} else {
    MessageBox.Show("Multiple "+animal+"s");
}

您可以使用
SelectNodes
作为
XmlNodeList
,然后检查其节点计数。如果计数为零,则表示“未找到节点”;否则,抓取属性并打印它:

XmlDocument xDoc = new XmlDocument();
xDoc.Load("animals.xml");
string animal = "Tiger";
XmlNodeList theList = xDoc.SelectNodes("/Root/Animal[@value='" + animal + "']");
if (theList.Count == 1) {
    MessageBox.Show(Convert.ToString(theList[0].Attributes["value"].InnerText));
} else if (theList.Count == 0) {
    MessageBox.Show("No "+animal);
} else {
    MessageBox.Show("Multiple "+animal+"s");
}
差不多

var animalNode = xDoc.SelectSingleNode("/Root/Animal[@value='" + animal + "']");
if (animalNode != null)
{
  var valueAttr = animalNode.Attributes["value"];
  if (valueAttr != null)
  {  
     MessageBox.Show(valueAttr.InnerText); //.value ???
  }
}
差不多

var animalNode = xDoc.SelectSingleNode("/Root/Animal[@value='" + animal + "']");
if (animalNode != null)
{
  var valueAttr = animalNode.Attributes["value"];
  if (valueAttr != null)
  {  
     MessageBox.Show(valueAttr.InnerText); //.value ???
  }
}

您可以使用Linq to Xml获取动物的名称(如果未找到动物,它将返回
null
):

此外,您还可以简单地检查是否找到任何与您的值匹配的动物,并仅在存在匹配项时获取名称:

var xpath = String.Format("Root/Animal[@value='{0}']", animal);
var animalElement = xdoc.XPathSelectElement(xpath);

if (animalElement != null)
    MessageBox.Show((string)animalElement.Attribute("name"));

您可以使用Linq to Xml获取动物的名称(如果未找到动物,它将返回
null
):

此外,您还可以简单地检查是否找到任何与您的值匹配的动物,并仅在存在匹配项时获取名称:

var xpath = String.Format("Root/Animal[@value='{0}']", animal);
var animalElement = xdoc.XPathSelectElement(xpath);

if (animalElement != null)
    MessageBox.Show((string)animalElement.Attribute("name"));


提示是找出
xDoc.SelectSingleNode()
为您提供的不同值返回的值。在对其调用任何方法之前,请验证
xDoc.SelectSingleNode(“/Root/Animal[@value='”+Animal+“]]”)是否为
null
。您试图从xml中获取什么?只有一个动物节点。您正在尝试检查它的
value
属性是否具有指定的值?从我提供的代码中,我将返回“Elephant”@MeowMeow您正在传递
Elephant
,并希望返回
Elephant
此操作的目的是什么?提示是找出
xDoc.SelectSingleNode()
返回您提供的不同值。在对其调用任何方法之前,请验证
xDoc.SelectSingleNode(“/Root/Animal[@value='“+Animal+']”)是否为
null
。您试图从xml获取什么?只有一个动物节点。您正在尝试检查它的
属性是否具有指定的值?从我提供的代码中,我将返回“大象”@MeowMeow您正在通过
大象
,并希望返回
大象
此操作的目的是什么?使用System.Linq;使用System.Xml;使用System.Xml.XPath;我有这三个,但错误是:错误1实例参数:无法从'System.Xml.XmlDocument'转换为'System.Xml.Linq.XNode'error 2'System.Xml.XmlDocument'不包含'XPathSelectElement'的定义和最佳扩展方法重载'System.Xml.XPath.Extensions.XPathSelectElement(System.Xml.Linq.XNode,string)“有一些是无效的arguments@MeowMeow
xdoc
XDocument
,而不是
XmlDocument
。这就是Linq到Xml,最好用于Xml解析将其更改为XDocument仍然无法使用XPathSelectElement@MeowMeow确保使用System.Xml.XPath定义了命名空间
。在System.Xml.Xpath名称空间中定义的这个扩展方法是我使用System.Xml.Xpath添加的;使用System.Linq;使用System.Xml;使用System.Xml.XPath;我有这三个,但错误是:错误1实例参数:无法从'System.Xml.XmlDocument'转换为'System.Xml.Linq.XNode'error 2'System.Xml.XmlDocument'不包含'XPathSelectElement'的定义和最佳扩展方法重载'System.Xml.XPath.Extensions.XPathSelectElement(System.Xml.Linq.XNode,string)“有一些是无效的arguments@MeowMeow
xdoc
XDocument
,而不是
XmlDocument
。这就是Linq到Xml,最好用于Xml解析将其更改为XDocument仍然无法使用XPathSelectElement@MeowMeow确保使用System.Xml.XPath定义了命名空间
。在System.Xml.Xpath名称空间中定义的这个扩展方法是我使用System.Xml.Xpath添加的;