【发布时间】: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