连接xml文档

连接xml文档,xml,linq-to-xml,Xml,Linq To Xml,我已经创建了一个xml文档。。下面是我的代码 XDocument xDoc = new XDocument(); //SerializeToFile(getContentResponse, getContentResponse); for (int i = 0; i < getContentResponse.PresentationElements.Count(); i++) { xDoc = new XDocumen

我已经创建了一个xml文档。。下面是我的代码

XDocument xDoc = new XDocument();
        //SerializeToFile(getContentResponse, getContentResponse);
        for (int i = 0; i < getContentResponse.PresentationElements.Count(); i++)
        {
            xDoc = new XDocument(
                         new XDeclaration("1.0", "UTF-16", null),
                         new XElement("PresentationElements",
                             new XElement("PresentationElement",
                                 new XComment("Only 3 elements for demo purposes"),
                                 new XElement("ContentReference", getContentResponse.PresentationElements[i].ContentReference),
                                 new XElement("ID", getContentResponse.PresentationElements[i].ID),
                                 new XElement("Name", getContentResponse.PresentationElements[i].Name)
                                )
                                for (int j = 0; j < getContentResponse.PresentationElements[i].PresentationContents.Count(); j++)
        {


        xDoc+=    new XElement("PresentationContents",
                                    new XElement("PresentationContent",
                                        new XElement("Content", getContentResponse.PresentationElements[i].PresentationContents[j].Content),
                                        new XElement("ContentType", getContentResponse.PresentationElements[i].PresentationContents[j].ContentType),
                                        new XElement("Language", getContentResponse.PresentationElements[i].PresentationContents[j].Language),
                                        new XElement("Medium", getContentResponse.PresentationElements[i].PresentationContents[j].Medium)
                                        ))));
                                }
            //check if elments exists.
            StringWriter sw = new StringWriter();
            XmlWriter xWrite = XmlWriter.Create(sw);
            xDoc.Save(xWrite);
            xWrite.Close();
            xDoc.Save(@"C:\Users\aqutbuddin\Documents\Visual Studio 2010\Projects\GetCposOfferPresentationContent\GetCposOfferPresentationContent\Log\getContent.xml", SaveOptions.None);
            Console.WriteLine("Saved");
        }
XDocument xDoc=new XDocument();
//SerializeToFile(getContentResponse,getContentResponse);
for(int i=0;i
我实际上想做的是我的xml文件应该是这样的

    <PresentationElements>
    <PresentationElement>
      <ExtensionData />
      <ContentReference>Product View Pack</ContentReference>
      <ID>SHOPPING_ELEMENT:10400044</ID>
      <Name>View Pack PE</Name>
      <PresentationContents>
        <PresentationContent>
          <ExtensionData />
          <Content>View Pack</Content>
          <ContentType>TEXT</ContentType>
          <Language>ENGLISH</Language>
          <Medium>COMPUTER_BROWSER</Medium>
          <Name>Name</Name>
        </PresentationContent>
        <PresentationContent>
          <ExtensionData />
          <Content>Have more control of your home's security and lighting with View Pack from XFINITY Home.</Content>
          <ContentType>TEXT</ContentType>
          <Language>ENGLISH</Language>
          <Medium>COMPUTER_BROWSER</Medium>
          <Name>Description</Name>
        </PresentationContent>
        <PresentationContent>
          <ExtensionData />
          <Content>/images/shopping/devices/xh/view-pack-2.jpg</Content>
          <ContentType>TEXT</ContentType>
          <Language>ENGLISH</Language>
          <Medium>COMPUTER_BROWSER</Medium>
          <Name>Image</Name>
        </PresentationContent>
        <PresentationContent>
          <ExtensionData />
          <Content>The View Pack includes:
2 Lighting / Appliance Controllers
2 Indoor / Outdoor Cameras</Content>
          <ContentType>TEXT</ContentType>
          <Language>ENGLISH</Language>
          <Medium>COMPUTER_BROWSER</Medium>
          <Name>Feature1</Name>
        </PresentationContent>
      </PresentationContents>
    </PresentationElement>
</PresentationElements>

产品视图包
购物单元:10400044
视图包PE
视图包
正文
英语
计算机浏览器
名称
使用XFINITY home的View Pack,您可以更好地控制家中的安全和照明。
正文
英语
计算机浏览器
描述
/图像/购物/设备/xh/view-pack-2.jpg
正文
英语
计算机浏览器
形象
视图包包括:
2个照明/设备控制器
2台室内/室外摄像机
正文
英语
计算机浏览器
特点1

有人能指出我做错了什么吗?那么解决方案应该是什么呢?

我想您应该用一个来自的
来替换
for
循环。。在创建内容的
表达式中:

xDoc = new XDocument(
                         new XDeclaration("1.0", "UTF-16", null),
                         new XElement("PresentationElements",
                             new XElement("PresentationElement",
                                 new XComment("Only 3 elements for demo purposes"),
                                 new XElement("ContentReference", getContentResponse.PresentationElements[i].ContentReference),
                                 new XElement("ID", getContentResponse.PresentationElements[i].ID),
                                 new XElement("Name", getContentResponse.PresentationElements[i].Name),
                                 from el in getContentResponse.PresentationElements[i].PresentationContents select

new XElement("PresentationContents",
                                    new XElement("PresentationContent",
                                        new XElement("Content", el.Content),
                                        new XElement("ContentType", el.ContentType),
                                        new XElement("Language", el.Language),
                                        new XElement("Medium", el.Medium)
                                        ))))
                                 ))));

我不确定您是否想用类似的方法替换外部循环,因为我不确定您是否想创建多个文档。

一个演示元素中可以有多个演示内容元素