使用联接的 LINQ to XML
我有一个采用以下格式的 XML 文件,其中 action type=0
是默认设置。
Actiontype 1 和 2 是覆盖设置。因此,只要 XML 中提供了类型 1 或类型 2 设置,它们就应该覆盖默认设置。
要覆盖默认 type=0 的字段 id,我尝试与覆盖 type=1 的字段 id 进行连接,以便我可以获得 type=1 值并在我的应用程序中使用它们。但是,连接似乎不起作用。有没有更好的方法来覆盖默认值?
Type=0 始终可用,但 Type=1 或 Type=2 将被传递。
还有另一种方法可以通过反射来做到这一点吗?
XML
<ActionTypes>
<ActionType Type="0">
<Field Id="Label1" Name="StartDate" ComparePreviousYear="False" CompareCurrentYear="True"></Field>
<Field Id="Label2" Name="EndDate" ComparePreviousYear="False" CompareCurrentYear="True"></Field>
<Field Id="Label3" Name="Cost" ComparePreviousYear="True" CompareCurrentYear="False"></Field>
<Field Id="Label4" Name="Total" ComparePreviousYear="False" CompareCurrentYear="False"></Field>
</ActionType>
<ActionType Type="1">
<Field Id="Label3" Name="Cost" ComparePreviousYear="True" CompareCurrentYear="True"></Field>
</ActionType>
<ActionType Type="2">
<Field Id="Label2" Name="EndDate" ComparePreviousYear="True" CompareCurrentYear="True"></Field>
</ActionType>
</ActionTypes>
代码
IEnumerable<XElement> defaultFields = from test in defaultElements.Elements()
where test.Attribute("Type").Value == "0"
select test;
IEnumerable<XElement> overrideFields = from test in defaultElements.Elements()
where test.Attribute("Type").Value == "1"
select test;
var overrideFields = from dflt in dftElements.Elements("Field")
join ovrd in ovrElements.Elements("Field") on dflt.Attributes("Id") equals ovrd.Attributes("Id")
select dflt,ovrd;
I have an XML file in the following format where action type=0
is the default settings.
Actiontype 1 and 2 are override settings. So whenever Type 1 or Type 2 settings are available in the XML they should override the default settings.
To override the field id's of the default type=0, I am trying to do a join with the field id of override type=1, so that I can get the type=1 values and use them within my application. Howvever, the join doesn't seem to work. Is there a better way to override the default values?
Type=0 is always available, but either Type=1 or Type=2 will be passed.
Is there another way to do this with reflection?
XML
<ActionTypes>
<ActionType Type="0">
<Field Id="Label1" Name="StartDate" ComparePreviousYear="False" CompareCurrentYear="True"></Field>
<Field Id="Label2" Name="EndDate" ComparePreviousYear="False" CompareCurrentYear="True"></Field>
<Field Id="Label3" Name="Cost" ComparePreviousYear="True" CompareCurrentYear="False"></Field>
<Field Id="Label4" Name="Total" ComparePreviousYear="False" CompareCurrentYear="False"></Field>
</ActionType>
<ActionType Type="1">
<Field Id="Label3" Name="Cost" ComparePreviousYear="True" CompareCurrentYear="True"></Field>
</ActionType>
<ActionType Type="2">
<Field Id="Label2" Name="EndDate" ComparePreviousYear="True" CompareCurrentYear="True"></Field>
</ActionType>
</ActionTypes>
Code
IEnumerable<XElement> defaultFields = from test in defaultElements.Elements()
where test.Attribute("Type").Value == "0"
select test;
IEnumerable<XElement> overrideFields = from test in defaultElements.Elements()
where test.Attribute("Type").Value == "1"
select test;
var overrideFields = from dflt in dftElements.Elements("Field")
join ovrd in ovrElements.Elements("Field") on dflt.Attributes("Id") equals ovrd.Attributes("Id")
select dflt,ovrd;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该在此处使用
Attribute
而不是Attributes
:事实上,您正在比较两个
IEnumerable
是否相等,这将使用简单的参考比较 - 这永远不会是真的。You should use
Attribute
here rather thanAttributes
:As it is, you're comparing two
IEnumerable<XAttribute>
for equality, which will use a simple reference comaprison - which will never be true.如果我正确理解了你的要求,这似乎可行
This seems to work if I have understood your requirement correctly