使用联接的 LINQ to XML

发布于 2024-09-06 13:12:24 字数 2047 浏览 3 评论 0原文

我有一个采用以下格式的 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 技术交流群。

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

发布评论

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

评论(2

悲喜皆因你 2024-09-13 13:12:24

您应该在此处使用 Attribute 而不是 Attributes

join ovrd in ovrElements.Elements("Field")
    on dflt.Attributes("Id") equals ovrd.Attributes("Id")

事实上,您正在比较两个 IEnumerable 是否相等,这将使用简单的参考比较 - 这永远不会是真的。

You should use Attribute here rather than Attributes:

join ovrd in ovrElements.Elements("Field")
    on dflt.Attributes("Id") equals ovrd.Attributes("Id")

As it is, you're comparing two IEnumerable<XAttribute> for equality, which will use a simple reference comaprison - which will never be true.

叶落知秋 2024-09-13 13:12:24

如果我正确理解了你的要求,这似乎可行

   class Program
    {
        static void Main(string[] args)
        {
            XDocument xd = XDocument.Load("Test.xml");
            var defaultElements = xd.Document.Element("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 ovFields = from dflt in defaultFields.Elements("Field")
                           join x in overrideFields.Elements("Field") on dflt.Attribute("Id").Value equals x.Attribute("Id").Value
                           into g
                           from ovrd in g.DefaultIfEmpty()
                           select (ovrd == null ? dflt : ovrd);
            foreach (var x in ovFields.SelectMany(y=>y.Attributes()))
                Console.WriteLine("{0}           {1}", x.Name, x.Value);

            Console.ReadLine();

        }



    }

This seems to work if I have understood your requirement correctly

   class Program
    {
        static void Main(string[] args)
        {
            XDocument xd = XDocument.Load("Test.xml");
            var defaultElements = xd.Document.Element("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 ovFields = from dflt in defaultFields.Elements("Field")
                           join x in overrideFields.Elements("Field") on dflt.Attribute("Id").Value equals x.Attribute("Id").Value
                           into g
                           from ovrd in g.DefaultIfEmpty()
                           select (ovrd == null ? dflt : ovrd);
            foreach (var x in ovFields.SelectMany(y=>y.Attributes()))
                Console.WriteLine("{0}           {1}", x.Name, x.Value);

            Console.ReadLine();

        }



    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文