C# 创建复杂的xml结构

C# 创建复杂的xml结构,c#,xml,c#-3.0,C#,Xml,C# 3.0,我是一名只有1年经验的C开发人员。我在为web服务创建复杂的xml请求时遇到了一个问题 我已经收到XSD文件&示例xml请求。现在我必须用正确的数据填充它并调用web服务。 我在创建小型xml结构方面有一些经验。 我所做的是使用字符串concatation/xml文档对象/xml编写器。 这些方法适用于小型结构,但对于大型结构,使用上述对象模型编写每个标记并不容易 请告诉我从C创建复杂xml结构的最佳方法。谢谢。Linq to Xml是从Linq查询中表达Xml的一种非常简洁的方法 以下是如何在

我是一名只有1年经验的C开发人员。我在为web服务创建复杂的xml请求时遇到了一个问题

我已经收到XSD文件&示例xml请求。现在我必须用正确的数据填充它并调用web服务。 我在创建小型xml结构方面有一些经验。 我所做的是使用字符串concatation/xml文档对象/xml编写器。 这些方法适用于小型结构,但对于大型结构,使用上述对象模型编写每个标记并不容易


请告诉我从C创建复杂xml结构的最佳方法。谢谢。

Linq to Xml是从Linq查询中表达Xml的一种非常简洁的方法

以下是如何在Microsoft的LINQ to Xml中构建Xml树:

输出:

<Contacts>
  <Contact>
    <Name>Patrick Hines</Name>
    <Phone>206-555-0144</Phone>
    <Address>
      <Street1>123 Main St</Street1>
      <City>Mercer Island</City>
      <State>WA</State>
      <Postal>68042</Postal>
    </Address>
  </Contact>
</Contacts>
约瑟夫·阿尔巴哈里(Joseph Albahari)的《c圣经》中有一些很好的例子,包括关于Linq到XML的一章。他的免费应用程序附带了一些很好的例子,如下面第10章的例子

下面的示例直接从Linq语句构建XML。您可以看到,与直接序列化相比,它为您提供了对输出XML更直接的控制,并简化了创建更复杂的XML结构

// Query Example 1

IQueryable<XElement> sqlQuery =
from c in Customers
    select 
        new XElement ("customer", new XAttribute ("id", c.ID),
            new XElement ("name", c.Name),
            new XElement ("buys", c.Purchases.Count)
        );
var customers = new XElement ("customers", sqlQuery);



// Query Example 2

new XElement ("customers",
from c in Customers
    let lastBigBuy = (
        from p in c.Purchases
        where p.Price > 1000
        orderby p.Date descending
        select p
    ).FirstOrDefault()
    select 
    new XElement ("customer", new XAttribute ("id", c.ID),
        new XElement ("name", c.Name),
        new XElement ("buys", c.Purchases.Count),
        new XElement ("lastBigBuy",
            new XElement ("description",
                lastBigBuy == null ? null : lastBigBuy.Description),
            new XElement ("price",
                lastBigBuy == null ? 0m : lastBigBuy.Price)
            )
        )
    )

试试这个:这里没有足够的信息来帮助你。请展示您创建的代码以及失败的示例,包括任何错误XmlSerializer@MikeN它有30多个节点,并且有很多子节点。我仍然没有创建要序列化的类,因为创建映射xml的类也很困难。它也有很多子对象。还有一些帖子说,我们不应该使用serialize。谢谢。@paqogomez我还没有写代码。我正在寻找最好的方法。如果我使用xml文档、XElement或xml编写器,那么我需要为每个标记编写代码。因此,很难为具有30个节点的xml的复杂结构编写代码。请让我知道这是唯一的方法还是有一个简单的方法来创建那个xml的类对象。但是我的xml是一个复杂的xml,它有太多的子对象。因此,如果我尝试这种方法,它将花费大量的时间,需要仔细注意丢失的标签。这是创建xml结构或映射到c对象模型的最佳方法,还是除了手动创建之外还有其他简单方法。另一种可能是使用上面DeepTownCitizens链接中解释的复杂对象序列化。添加了直接从linq语句生成的更高级版本。
// Query Example 1

IQueryable<XElement> sqlQuery =
from c in Customers
    select 
        new XElement ("customer", new XAttribute ("id", c.ID),
            new XElement ("name", c.Name),
            new XElement ("buys", c.Purchases.Count)
        );
var customers = new XElement ("customers", sqlQuery);



// Query Example 2

new XElement ("customers",
from c in Customers
    let lastBigBuy = (
        from p in c.Purchases
        where p.Price > 1000
        orderby p.Date descending
        select p
    ).FirstOrDefault()
    select 
    new XElement ("customer", new XAttribute ("id", c.ID),
        new XElement ("name", c.Name),
        new XElement ("buys", c.Purchases.Count),
        new XElement ("lastBigBuy",
            new XElement ("description",
                lastBigBuy == null ? null : lastBigBuy.Description),
            new XElement ("price",
                lastBigBuy == null ? 0m : lastBigBuy.Price)
            )
        )
    )