Linq to XML...我从来没有按原样使用 XML 获得任何后代值

发布于 2024-09-26 12:46:44 字数 2071 浏览 3 评论 0原文

这是我试图解析的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唔猫 2024-10-03 12:46:44

您需要一个像这样的XNamespace

XNamespace ns = "http://soap.sforce.com/2006/04/metadata";
var setting = xmlDoc.Root.Descendants(ns+"rules").First(e => e.Element(ns+"fullName").Value == "No Service Contact Assigned");
Console.WriteLine(setting.Element(ns+"active").Value); 

请注意,文档不是根节点(例如Workflow 元素),但文档上的Root 属性是。我还补充说。

You need an XNamespace like this:

XNamespace ns = "http://soap.sforce.com/2006/04/metadata";
var setting = xmlDoc.Root.Descendants(ns+"rules").First(e => e.Element(ns+"fullName").Value == "No Service Contact Assigned");
Console.WriteLine(setting.Element(ns+"active").Value); 

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.

你可以这样做:

        var setting = xmlDoc.Descendants(XName.Get("rules", "http://soap.sforce.com/2006/04/metadata")).First(e => e.Element(XName.Get("fullName", "http://soap.sforce.com/2006/04/metadata")).Value == "No Service Contact Assigned");
        Console.WriteLine(setting.Element(XName.Get("active", "http://soap.sforce.com/2006/04/metadata")).Value);

You can do it like this:

        var setting = xmlDoc.Descendants(XName.Get("rules", "http://soap.sforce.com/2006/04/metadata")).First(e => e.Element(XName.Get("fullName", "http://soap.sforce.com/2006/04/metadata")).Value == "No Service Contact Assigned");
        Console.WriteLine(setting.Element(XName.Get("active", "http://soap.sforce.com/2006/04/metadata")).Value);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文