Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.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
Linq到XML:查询If块C#_C#_Xml_Linq - Fatal编程技术网

Linq到XML:查询If块C#

Linq到XML:查询If块C#,c#,xml,linq,C#,Xml,Linq,我已经像这样设置了XML <customers> <customer id="1"> <name title="Mr" first="John" last="Smith" /> <contact number="07123123123" email="john.smith@johnsmith.com" /> <address postcode="E1 1EW">1 Paper Street, London,

我已经像这样设置了XML

<customers>
  <customer id="1">
    <name title="Mr" first="John" last="Smith" />
    <contact number="07123123123" email="john.smith@johnsmith.com" />
    <address postcode="E1 1EW">1 Paper Street, London, England, GB</address>
  </customer>
  (...)
</customers>
无论我尝试什么,编译器都会嘲笑我,或者说它无法隐式地将Ienumerable布尔转换为布尔

一段时间以来,我一直在尝试从许多不同的谷歌搜索中进行多种组合,我认为猜测开始弊大于利,有人能为我的xml设置提供一个在if块中工作的C#代码段吗?只是想看看名字和姓氏是否同时存在于同一个节点中。通常,一旦我看到某些东西真的起作用了,我就可以从那里得到它。我在网上找到的大多数问题都只是为了查看整个节点是否存在,或者只是搜索一个属性,而我似乎无法对其进行裁剪

(在有人对我的XML结构大发雷霆之前,这不是为了生产目的,我只想掌握如何在即将到来的项目中使用它。)

任何人都可以链接非MSDN的文档(我已经打开了大约10个标签),并且包含一些关于Linq到XML的好的低级/初级教程。XPath是你的朋友

using System.Xml.XPath;
...

void foo(){
    XDocument document = XDocument.Load("People.xml");

    var firstCustomerNode = document.XPathSelectElement(
        "/customers/customer[0]/name"
        );
    var hasfirstNameAndLastName = firstCustomerNode.Attribute("firstname") != null && firstCustomerNode.Attribute("lastname") != null;

    if(hasfirstNameAndLastName)
    {

    }
}
请注意,您也可以使用模式来验证Xml,但编写起来更复杂

如果不想使用XPath,还可以编写:

var firstCustomerNode = document.Root.Element("Customers").Elements("customer").First().Element("name");

但老实说,XPath查询的可读性要高得多,并且将简化错误检查。此代码假设目标节点上的所有元素都存在。否则,您的代码将失败,出现不良的
NullReferenceException

来检查您将使用的XML中是否存在John Smith作为客户

XDocument doc = XDocument.Load(Path);
var customers = doc.Descendants("customer");    
//To Check for John Smith    
if (customers.Elements("name")
             .Any(E => E.Attribute("first").Value == "John" 
                    && E.Attribute("last").Value == "Smith"))
{
    //Do your thing
}

创建了一个名为check的布尔值来保存结果。 计算名为name的元素的属性First=john和last=smith的数量 然后将结果设置为check变量

顺便说一句,您的XML在John前面缺少一个“。这将给您带来问题:)


首先需要查询
Document.Root.subjects
,然后使用
IfExists

var nodes = document.Root.Descendants("customer");

bool IfExists(string FirstName, string LastName) {
    return nodes.Elements("name").Any(node => 
        node.Attribute("first").Value.Equals(FirstName) &&
        node.Attribute("last").Value.Equals(LastName));
}

如果属性丢失或包含空值,您可能需要添加异常处理。

您尝试了什么?这样我们不仅可以给出正确的答案,还可以告诉您哪里出了问题。您需要从查询
文档开始。Root
我完全理解这对您有帮助,但我一直在哪里如果我现在操纵它几个小时,然后出于沮丧删除它以尝试下一件事,我现在将无法详细说明我已经做了什么,因为我没有明确地跟踪它。无论如何,它可能是格式严重错误的代码。此方法在
System.Xml.Linq
程序集中定义。因此它是指向Xml…b的链接ut有关系吗?你会用螺丝刀画画吗?@newStackExchangeInstance:这应该可以很好地工作,为什么你认为这不起作用?如果我输入了一个拼写错误,请告诉我,我会更正它。在你的XPath示例中,lastname没有nullcheck。另外,我发现LINQ to XML更可读,但意见就是意见。我认为LINQ是m这本书读起来更漂亮。它比XPath(请参阅此处的性能差异部分:)还具有性能优势。非常感谢!您知道如何将邮政编码也包括在内吗?它比第一个/最后一个attrib提高了一个级别,但对客户来说是一样的。any(E=>E.Element(“name”).Attribute(“first”).Value==“John”和&E.Element(“address”))属性(“邮政编码”)。值==“E1 1EW”)似乎不起作用。您不能这样做。因为选择器E查看的是name元素,比如。您有一个customer.Elements(“name”)选择器。因此,要检查邮政编码,您必须提高一个级别,并执行以下操作:if(customers.Any(E=>E.Element(“name”).Attribute(“first”).Value==“John”和&E.Element(“name”).Attribute(“last”).Value==“Smith”和&E.Element(“address”).Attribute(“postcode”).Value==“E1 1EW”)我试过了,但似乎不起作用。它应该起作用吗?如果是这样的话,那么问题可能在我的代码中的其他地方,它似乎总是返回false(John Smith@e11ew不存在)。如果我垃圾邮件“添加”按钮,我已经设置它只是不断地添加新的约翰史密斯在E1 1EW没有消息框。显示解雇警告,这些细节已经存在。
bool check;
var res = XDocument.Load(@"c:\temp\test.xml");
var results = res.Descendants("name")
                 .Where(x => x.Attribute("first").Value == "john" && x.Attribute("last").Value == "smith")
                 .Select(x => x.Elements()).Count();

check = results != 0;
var nodes = document.Root.Descendants("customer");

bool IfExists(string FirstName, string LastName) {
    return nodes.Elements("name").Any(node => 
        node.Attribute("first").Value.Equals(FirstName) &&
        node.Attribute("last").Value.Equals(LastName));
}