【问题标题】:Change or parse CRM FetchXML with LINQ to XML使用 LINQ to XML 更改或解析 CRM FetchXML
【发布时间】:2012-03-09 12:05:46
【问题描述】:

我有一个 CRM FetchXML 字符串。这有一个链接实体,这意味着 XML 元素以 GUID 为前缀,例如:

 <a_6dd7d25cb0be4150bbaec7c0d2f430c7.lastname>Bob</a_6dd7d25cb0be4150bbaec7c0d2f430c7.lastname>
 <a_6dd7d25cb0be4150bbaec7c0d2f430c7.firstname>Bob</a_6dd7d25cb0be4150bbaec7c0d2f430c7.firstname> 
 <a_6dd7d25cb0be4150bbaec7c0d2f430c7.title name="Mr" formattedvalue="1">1</a_6dd7d25cb0be4150bbaec7c0d2f430c7.title> 

我想以类似的方式使用它:

List<FetchResult> results =
            (from result in xmlDoc.Descendants("result")
             select new FetchResult
             {
                 FirstName = result.Element("firstname").Value
             }).ToList<FetchResult>();

显然这行不通,所以无论如何我可以做一个Element.Contains("lastname") 或类似的吗? 如果没有,有没有办法我可以事先从我的字符串中解析出来?也许是正则表达式?

谢谢

【问题讨论】:

    标签: c# xml regex linq


    【解决方案1】:
    List<FetchResult> results =
                (from result in xmlDoc.Descendants("result")
                 select new FetchResult
                 {
                     FirstName = result.Elements().First(el => el.EndsWith("firstname")).Value
                 }).ToList<FetchResult>();
    

    应该有效

    【讨论】:

    • +1 谢谢你 - 它并没有很好地工作,但把我推向了正确的方向。我现在将添加正确的语法。再次感谢。
    【解决方案2】:

    感谢Foo42 - 这是更新的语法

    FirstName = result.Elements().First(el => el.Name.ToString().EndsWith("firstname")).Value
    

    【讨论】:

    • 哦,是的,Elements 是一种方法,不是吗!
    猜你喜欢
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多