Linq to XML...我从来没有按原样使用 XML 获得任何后代值
这是我试图解析的 xml 的一部分:
<?xml version="1.0" encoding="UTF-8"?>
<Workflow xmlns="http://soap.sforce.com/2006/04/metadata">
<alerts>
<fullName>Broker_Not_In_SF_Current_Broker_Account</fullName>
<description>Broker Not In SF - Current Broker Account</description>
<protected>false</protected>
<recipients>
<recipient>[email protected]</recipient>
<type>user</type>
</recipients>
<senderType>CurrentUser</senderType>
<template>Broker_Emails/Current_Broker_on_Acct_Not_in_SF</template>
</alerts>
<rules>
<fullName>No Service Contact Assigned</fullName>
<active>true</active>
<criteriaItems>
<field>Account.Type</field>
<operation>equals</operation>
<value>Client</value>
</criteriaItems>
<criteriaItems>
<field>Account.Service_Contact__c</field>
<operation>equals</operation>
</criteriaItems>
<description>Notification when a service contact has not been assigned after 5 days, then 8 days.</description>
<triggerType>onCreateOrTriggeringUpdate</triggerType>
</rules>
</Workflow>
我可以在一些随机的 SIMPLE XML 文件上使用简单的 Linq to XML,但在这种情况下我什么也没得到。我假设是因为 xml 命名空间,但是除了在解析之前在 xml 规则中删除它之外,还有其他方法可以解决这个问题吗?
我过去没有做过太多 XML 工作,所以我试图了解如何获取如图所示的文件并提取规则节点来创建具有属性的集合或规则。我可以获得对象/集合部分,但我一直困惑为什么命名空间无法完成这个简单的调用:
var setting = xmlDoc.Descendants("rules").First(e => e.Element("fullName").Value == "No Service Contact Assigned");
Console.WriteLine(setting.Element("active").Value);
感谢您提供有关如何处理此问题或如何正确使用命名空间的帮助。
Here is part of the xml I am trying to parse:
<?xml version="1.0" encoding="UTF-8"?>
<Workflow xmlns="http://soap.sforce.com/2006/04/metadata">
<alerts>
<fullName>Broker_Not_In_SF_Current_Broker_Account</fullName>
<description>Broker Not In SF - Current Broker Account</description>
<protected>false</protected>
<recipients>
<recipient>[email protected]</recipient>
<type>user</type>
</recipients>
<senderType>CurrentUser</senderType>
<template>Broker_Emails/Current_Broker_on_Acct_Not_in_SF</template>
</alerts>
<rules>
<fullName>No Service Contact Assigned</fullName>
<active>true</active>
<criteriaItems>
<field>Account.Type</field>
<operation>equals</operation>
<value>Client</value>
</criteriaItems>
<criteriaItems>
<field>Account.Service_Contact__c</field>
<operation>equals</operation>
</criteriaItems>
<description>Notification when a service contact has not been assigned after 5 days, then 8 days.</description>
<triggerType>onCreateOrTriggeringUpdate</triggerType>
</rules>
</Workflow>
I can use simple Linq to XML on some random SIMPLE XML file, but In this instance I get nothing. I assume because of the xml namespace, but is there a way around this other than removing that in the xml rile prior to parsing?
I haven't done much XML work in the past, so I'm trying to understand the best way that I can take a file like shown and pull out the rule nodes to create a collection or rules with properties. I can get the object/collection part, but I'm stuck on WHY the namespace is failing this simple call:
var setting = xmlDoc.Descendants("rules").First(e => e.Element("fullName").Value == "No Service Contact Assigned");
Console.WriteLine(setting.Element("active").Value);
Thanks for any help on what to do about this or how to properly use the namespace.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要一个像这样的
XNamespace
:请注意,文档不是根节点(例如
Workflow
元素),但文档上的Root 属性是。我还补充说。You need an
XNamespace
like this:Note that the document is not the root node (e.g.
Workflow
element), but the Root property on the document is. I also added that.你可以这样做:
You can do it like this: