2013-04-29 3 views
0

Я создал документ xml .. ниже мой код.Конкатенация документа 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"); 
     } 

Что я на самом деле хочу сделать, так это то, что мой 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> 

Можно ли предположить, что я делаю неправильно? И каково должно быть решение?

ответ

0

Я думаю, что вы хотите, чтобы заменить петлю for на from .. in выражение, которое создает содержание:

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) 
             )))) 
           )))); 

Я не уверен даже, хотите ли вы заменить внешний контур с подобным подходом, как я не являюсь убедитесь, что вы хотите создать несколько документов.

+0

да, я хочу создать несколько документов .. –

+0

может быть несколько элементов содержимого презентации в элементе представления –

Смежные вопросы