【问题标题】:LINQ to XML, with conditions and Variable XML Structure with C#LINQ to XML,带有条件和带有 C# 的可变 XML 结构
【发布时间】:2015-07-14 16:08:07
【问题描述】:

我有一个大问题,我花了大约 5 个小时来解决这个问题。 我必须创建一个 XML 文件,该文件将由数据库 reg 填充。 我有可变结构,在某些情况下会使用特定的子结构。

逻辑很简单。

我必须得到所有的代理, 然后在每个机构中,我必须迭代一个 foreach 循环, 并获得他们想要出售/出租的所有房屋。 但它不仅有房子,还有公寓、车库等等。 每个场景都有自己的结构和不同的数据。

这不是全部,必须有一个条件。仅在应该写入 xml 子项时才写入。

一段xml示例

<Clients>
    <Client>
        <aggregator/>
        <code/>
        <reference/>
        <contact>
        <secondhandListing>
            <property>
                <code/>
                <reference/>
                <scope/>
                <address>
                <features/>
                <operation>     // variable structure

    1example        <price/>
                    <communityCosts/>

    2example       <price/>
                   <communityCosts/>
                   <depositType/>

                </operation>
            </property>
        </secondhandListing>

谁能给我一个例子来说明如何做到这一点。 到目前为止我存档的是:

var agenciasConectores = //query of Agencys

foreach (var agenciaConector in agenciasConectores)
{
    var imoveisAgencia = // query of homes in each Agency

    XDocument doc = new XDocument(
        new XDeclaration("1.0", "utf-8", "yes"),
        new XElement("Clients",
            from agencia in agenciasConectores
                select new XElement("Client", new XAttribute("ID", agencia.AgencyId),
                new XElement("aggregator", agencia.ConnectorLicense),
                new XElement("code", agencia.Name),
                new XElement("Reference", agencia.Phone),
                new XElement("contact", agencia.Phone),
                new XElement("secondhandListing"),
                new XElement("newbuildListing")
                )));
    foreach (var imovel in imoveisAgencia)
    {
        if (imoveisAgencia.Count() > 1)
        {
            doc.Document.Add(new XElement("property",
                new XElement("code", "codigo"),
                new XElement("reference", "reference"),
                new XElement("scope", "scope"),
                new XElement("address", "address"),
                new XElement("contact", "contact"),
                new XElement("features", "features"),
                new XElement("operation", "operation"),
                new XElement("description", "description")));
        }

    }

}

【问题讨论】:

  • 你目前的工作出了什么问题?

标签: c# xml linq data-structures linq-to-xml


【解决方案1】:

当我尝试在 Visual Studio 中运行它时,以下行会引发异常

doc.Document.Add(new XElement("property",
...

您是否尝试做更多类似的事情?

doc.Root.Add(new XElement("property",
...

或者,也许,更接近这个

doc.Descendants("Client")
    .Single(c => c.code == "something")
    .Add(new XElement("property",
    ...

【讨论】:

    【解决方案2】:

    已经找到解决办法了。

    很简单。

    XElement root = new XElement("root");
    XElement child = new XElement("Child");
    XElement child2 = new XElement(Child2);
    

    然后我填充元素。

    Child.SetValue(a);
    child2.Setvalue(b);
    

    最后,我只是将元素添加到父元素。

    root.add(Child,Child2);
    

    但是这样我也可以进行一些验证(在我的情况下是循环)

    If(a>b)
        root.add(child);
    else
        root.add(child2);
    

    感谢您的尝试。我是通过例子学习的嘿嘿嘿

    【讨论】:

      猜你喜欢
      • 2011-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-17
      相关资源
      最近更新 更多