LINQ to XML 查询中的 xmlns 属性
Posted
技术标签:
【中文标题】LINQ to XML 查询中的 xmlns 属性【英文标题】:xmlns attribute in LINQ to XML query 【发布时间】:2021-07-27 08:21:18 【问题描述】:我在我的 VB.NET Programm 中做了以下函数...
Function ReturnTranslation(ByVal CGID As String) As String
Dim filename2 As String = "D:\\EBSDEMO_4.10.26.1\\CSMapProfiles\\BusinessRules\\ESGOPerson.xml"
Dim Doc As XDocument = XDocument.Load(filename2)
Dim query2 As IEnumerable(Of XElement) = From element In Doc.<BRDatasetNode>.<Translations>.<ESCaptionsDS>.<Captions>
Where element.Name = "Captions" 'And element.@ID = CGID.ToString
Select element
For Each myelement As XElement In query2
Dim currentElement As XElement = myelement
Dim MyFirstNode As XElement = currentElement.FirstNode
Dim MyLastNode As XElement = currentElement.LastNode
If MyFirstNode.Value = CGID Then
Return MyLastNode.Value
End If
Next
End Function
当下面的 XML 在元素 ESCaptionsDS 中具有命名空间属性 xmlns="http://tempuri.org/ESCaptionsDS.xsd" 然后函数内的 query2 集合返回 --> 枚举没有结果!
当我删除这个 xmlns 属性时,一切似乎都正常!
我能做什么?
<?xml version="1.0" encoding="UTF-8"?>
<BRDatasetNode RootNodeType="Entersoft.Framework.Platform.CustomCommands.BRDatasetNode, ESCustomNode" Version="2" CGID="42d7a588-adb7-4bf1-8ed0-fdefe8038267" Dataset="ESGOPerson">
<system_rules CGID="424034df-a434-4ebf-afe8-b95de8ab380e" />
<custom_rules CGID="b588a8e2-6ad5-4969-a171-c286da40355d">
<CustomRule CGID="998fd4ed-2ec4-4e33-97cc-354756aa584b">
<Elems CGID="fbf4c9c6-17e3-4dff-9797-ebdf44fdfe89">
<BRElemNode CGID="7fcbefe0-4922-4b74-8b77-1355042c2fe1" table="ESGOPerson">
<conditions CGID="0a5a66dc-e027-4475-a188-21cccf43dd6f" />
<ValidityConditions CGID="28ed1a57-ae38-4d83-87ea-b26f168a44f0" />
<ExecuteMessageConditions CGID="07e6f2b5-0649-4517-80ca-65a21df560d6" />
<OnAboutToSaveTriggerNode CGID="95ecaad6-71f0-4498-af97-f36f66232bf1" />
<InternalProfileActionNode CGID="d9e41736-c48d-4d0e-b368-54750c175a15">
<Profile CGID="b5ab3d08-bd96-4c7e-b6d2-6e54703e89f6" UILess="True" Identity="True" Save="False">
<SourceEntity EntityID="ESGOPerson" />
<TargetEntity EntityID="ESGOPerson" />
<SaveExpression CGID="2c139b9a-abad-4b06-a33e-dc2e22d686ab">
<Supplier>
<EnumValue Type="Entersoft.Framework.Platform.Helper.SaveEnum, ESEntity" Value="SAVE_AND_ACTIONS" />
</Supplier>
</SaveExpression>
<RootRule CGID="8a68da7b-04ca-4ed8-b592-8b7835b6b99f" Source="ESGOPerson" Target="ESGOPerson">
<ExecuteSimpleConditions CGID="dbfabc14-4b02-422e-b0a4-80e87934372d" />
<Variables CGID="3576cddf-157d-40c6-8cc2-35891551f49c" />
<Rules CGID="3a9186c2-7d84-4afb-8a36-afe8a6fc00fc" />
<Conditions CGID="8b0f9519-4ada-4733-bde2-654d492604c4">
<Condition CGID="690eae9a-34f0-4217-b842-586d7c8a8baf">
<Supplier>
<SourceValue>MultilingualName</SourceValue>
</Supplier>
<FormatParams CGID="3c9f3b4c-7ef2-4014-ac46-105d5ca973d7" />
</Condition>
</Conditions>
<Assignments CGID="e47ba8c3-acba-44d4-9747-6de3c4ceb30a">
<Assign CGID="08568b8b-2281-4ad1-adf0-0525f905d62b" Column="EORINumber">
<Supplier>
<ConstantValue>From BR !!</ConstantValue>
</Supplier>
<Conditions CGID="f4ac41a7-4fe6-462c-beaf-543daf8335fd" />
</Assign>
</Assignments>
<OrderByNodes CGID="e5fbe9a8-00f9-44dd-8e09-aec2a8868438" />
<GroupByNodes CGID="2ef5fed2-9c6e-426c-bac5-68be34b9abd2">
<GroupAtMostEntries CGID="e6717931-55b0-4ff8-8ad9-a4401699b944">
<Supplier>
<ConstantValue>0</ConstantValue>
</Supplier>
</GroupAtMostEntries>
</GroupByNodes>
<MultiplyByNodes CGID="acd4895a-901d-4efc-952b-714c429b415a" />
<RowActions CGID="abb2a62c-3ae1-416d-823a-56b98fbc3cab" />
</RootRule>
<MapExceptions CGID="7705c048-95bc-45bd-a98b-5036c8a02b9c" />
<LoginCredentials CGID="6996bb1c-4553-4ff2-bed1-1379e14ee362" />
</Profile>
</InternalProfileActionNode>
</BRElemNode>
<BRElemNode CGID="d4f8479b-efb4-40ef-aa6f-18f62fe85462" table="ESGOPerson">
<conditions CGID="02f0260e-a227-48b9-b1ee-5ba35c561002" />
<ValidityConditions CGID="734ce376-b5b6-4c20-b3ea-432f39b61fc1" />
<ExecuteMessageConditions CGID="188f609c-3aba-4b7e-8999-208c33063479" />
<OnAboutToSaveTriggerNode CGID="8ece5eb9-da37-432d-95bf-42a4cf9b2259" />
<InternalProfileActionNode CGID="153f64c6-145d-43a6-abd3-9142a3debf6f">
<Profile CGID="9576c174-b5e8-41a7-b0b1-36dd3bd085ba" UILess="True" Identity="True" Save="False">
<SourceEntity EntityID="ESGOPerson" />
<TargetEntity EntityID="ESGOPerson" />
<SaveExpression CGID="25730bbd-219c-41a7-b7cd-91b905527cc9">
<Supplier>
<EnumValue Type="Entersoft.Framework.Platform.Helper.SaveEnum, ESEntity" Value="SAVE_AND_ACTIONS" />
</Supplier>
</SaveExpression>
<RootRule CGID="3c2b9be0-5900-4216-b349-9caaeafe854a" Source="ESGOPerson" Target="ESGOPerson" TargetKey="GID">
<ExecuteSimpleConditions CGID="bef8d8ed-3c03-46cf-b59b-7579b38819dc" />
<Variables CGID="1cd5fe30-c781-4428-af38-a3a11e461e33" />
<Rules CGID="96995077-2740-4164-a4ac-3660eac4e8e3" />
<Conditions CGID="72c10ea6-cca2-4997-9b14-316a98215a41" />
<Assignments CGID="e1334ab7-c041-40da-810c-9136c2955732" />
<OrderByNodes CGID="2ec07a79-0d67-4cf7-b6e0-d049b43a3104" />
<GroupByNodes CGID="b9e8d2fc-318a-40ff-b6ac-385997197198">
<GroupAtMostEntries CGID="d71fe3cd-b2a3-484a-812d-c4e9442ae6ec">
<Supplier>
<ConstantValue>0</ConstantValue>
</Supplier>
</GroupAtMostEntries>
</GroupByNodes>
<MultiplyByNodes CGID="856b9e25-507f-4427-993f-7a9476bfd028" />
<RowActions CGID="6afe3d4d-3d68-4825-adce-94985de6f23b">
<RowAction CGID="e47f3afd-76eb-4f85-8bb2-5a6ad19c9e8b" ExecLevel="Dataset" trigger="TargetOnPreSave">
<Conditions CGID="6fcea0eb-9665-4ed4-9da0-00f5160d8d17" />
<CommandRowAction Type="ExternalProfileRowAction">
<profile_name CGID="872fe616-5287-472e-ad2c-63f1d2eeefdd">
<Supplier>
<ConstantValue>CSFormCommands\ESGOPerson\AutomationSetRemarkswithAsk.xml</ConstantValue>
</Supplier>
<CommandParameters CGID="158de9df-14ca-4c19-899e-a049a7129526" ParamName="RemarksStr">
<Supplier>
<ConstantValue>χχχχχ</ConstantValue>
</Supplier>
</CommandParameters>
</profile_name>
</CommandRowAction>
</RowAction>
</RowActions>
</RootRule>
<MapExceptions CGID="ffb15bbe-e6f6-4394-ae3b-58260e36aa39" />
<LoginCredentials CGID="09c389df-cb46-4a1e-8904-4dce59784300" />
</Profile>
</InternalProfileActionNode>
</BRElemNode>
</Elems>
<Conditions CGID="af050b88-7422-4f16-b4cc-cd070debcc2c" />
<ValidityConditions CGID="d0d56686-3c70-498c-8efd-21d991435e14" />
<ExecuteMessageConditions CGID="e8f33bdb-bad7-4547-9527-052cec29cd17" />
</CustomRule>
</custom_rules>
<Translations>
<ESCaptionsDS xmlns="http://tempuri.org/ESCaptionsDS.xsd">
<Captions>
<ID>998fd4ed-2ec4-4e33-97cc-354756aa584b</ID>
<Suffix>Title</Suffix>
<LangID>el</LangID>
<Caption>Διακριτικός Τίτλος</Caption>
</Captions>
<Captions>
<ID>7fcbefe0-4922-4b74-8b77-1355042c2fe1</ID>
<Suffix>Title</Suffix>
<LangID>el</LangID>
<Caption>Διακριτικός τίλτλος</Caption>
</Captions>
<Captions>
<ID>b5ab3d08-bd96-4c7e-b6d2-6e54703e89f6</ID>
<Suffix>Title</Suffix>
<LangID>el</LangID>
<Caption>Πρ το Πρ</Caption>
</Captions>
<Captions>
<ID>8a68da7b-04ca-4ed8-b592-8b7835b6b99f</ID>
<Suffix>Title</Suffix>
<LangID>el</LangID>
<Caption>Πρ το Πρ</Caption>
</Captions>
<Captions>
<ID>e6717931-55b0-4ff8-8ad9-a4401699b944</ID>
<Suffix>Title</Suffix>
<LangID>el</LangID>
<Caption>Maximum Entries</Caption>
</Captions>
<Captions>
<ID>d4f8479b-efb4-40ef-aa6f-18f62fe85462</ID>
<Suffix>Title</Suffix>
<LangID>el</LangID>
<Caption>Εκτελεση εκωτ. αυτοματισμού</Caption>
</Captions>
<Captions>
<ID>9576c174-b5e8-41a7-b0b1-36dd3bd085ba</ID>
<Suffix>Title</Suffix>
<LangID>el</LangID>
<Caption>Εκτελεση αυτοματισμού</Caption>
</Captions>
<Captions>
<ID>3c2b9be0-5900-4216-b349-9caaeafe854a</ID>
<Suffix>Title</Suffix>
<LangID>el</LangID>
<Caption>εκτέλεση αυτοματισμού</Caption>
</Captions>
<Captions>
<ID>d71fe3cd-b2a3-484a-812d-c4e9442ae6ec</ID>
<Suffix>Title</Suffix>
<LangID>el</LangID>
<Caption>Maximum Entries</Caption>
</Captions>
<Captions>
<ID>e47f3afd-76eb-4f85-8bb2-5a6ad19c9e8b</ID>
<Suffix>Title</Suffix>
<LangID>el</LangID>
<Caption>Εκτ αυτο</Caption>
</Captions>
</ESCaptionsDS>
</Translations>
</BRDatasetNode>
【问题讨论】:
不清楚你需要在函数中做什么。是根据<ID>
元素值匹配到一个函数参数——CGID,返回`这是默认命名空间。不带前缀的后代元素隐式继承此默认命名空间。您可以声明一个引用默认命名空间的前缀,并使用从ESCaptionsDS
到Captions
的前缀,例如:
Imports <xmlns:d="http://tempuri.org/ESCaptionsDS.xsd">
...
Dim query2 As IEnumerable(Of XElement) = From element In Doc.<BRDatasetNode>.<Translations>.<d:ESCaptionsDS>.<d:Captions>.<d:Caption>
Select element
dotnetfiddle demo
【讨论】:
感谢您的快速回复,但是当我执行您提到的解决方案时,我仍然遇到同样的问题。我完全按照你的建议做了......仍然得到枚举没有产生任何结果,当我从 ESCaptionDS 元素中删除 xmlns="tempuri.org/ESCaptionsDS.xsd" 时,一切正常...... 只是想确认一下,您是否使用了添加Imports
旁边的前缀?这部分准确地说:From element In Doc.<BRDatasetNode>.<Translations>.<d:ESCaptionsDS>.<d:Captions>
@TedKonstantoudakis 好的,我必须删除Where
子句,因为.<d:Caption>
部分的意图是多余的,而且它也是错误的,因为该元素位于命名空间中。我还添加了一个链接来证明代码有效【参考方案2】:
请尝试以下解决方案。 您可以将其用作功能的基础。
显示如下:
如何定义默认命名空间。 如何在 LINQ to XML 中使用命名空间。 如何在一条语句中查询 XML 而没有任何循环。VB.NET
Sub Main
Dim parameter As String = "d4f8479b-efb4-40ef-aa6f-18f62fe85462"
Const FILENAME As String = "e:\Temp\TedKonstantoudakis.xml"
Dim xdoc As XDocument = XDocument.Load(FILENAME)
Dim ns1 As XNamespace = "http://tempuri.org/ESCaptionsDS.xsd"
Dim Caption As String = xdoc.Descendants(ns1 + "Captions") _
.Where(Function(x) x.Element(ns1 + "ID").Value.Equals(parameter)) _
.Elements(ns1 + "Caption").FirstOrDefault()?.Value
Console.WriteLine("Caption='0'", Caption)
End Sub
输出
Caption='Εκτελεση εκωτ. αυτοματισμού'
【讨论】:
【参考方案3】:我在下面结束了。 谢谢大家!
Function ReturnTranslation2(ByVal CGID As String) As String
Dim ns1 As XNamespace = "http://tempuri.org/ESCaptionsDS.xsd"
Dim filename2 As String = "D:\\Temp\\File.xml"
Dim xdoc As XDocument = XDocument.Load(filename2)
Dim Caption As String = xdoc.Descendants(ns1 + "Captions").Where(Function(x) x.Element(ns1 + "ID").Value.Equals(CGID)).Elements(ns1 + "Caption").FirstOrDefault().Value
Return Caption
End Function
【讨论】:
这是@Miriamka 答案的副本。您需要将她的解决方案标记为答案。以上是关于LINQ to XML 查询中的 xmlns 属性的主要内容,如果未能解决你的问题,请参考以下文章
使用 linq to xml 从 xml 文档中删除 xmlns="something"
通过 LINQ 创建 XML 文档,添加 xmlns,xmlns:xsi