Add the xml attirbutes from one xml file to the second xml file ?

Last post 11-09-2009 10:46 AM by Martin_Honnen. 4 replies.

Sort Posts:

  • Add the xml attirbutes from one xml file to the second xml file ?

    11-07-2009, 3:59 PM
    • Member
      30 point Member
    • dotnetmate
    • Member since 12-12-2007, 4:59 PM
    • Posts 130

    Hi All,

    I have 2 xml files, I should be able to get the data from one xml file and append it to the tags in second xml file.

    the example is:

    First xml file:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"

    xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relation

    ships"><dimension ref="A1:C4"/><sheetViews><sheetView tabSelected="1"

    workbookViewId="0"><selection activeCell="A5"

    sqref="A5"/></sheetView></sheetViews><sheetFormatPr

    defaultRowHeight="15"/><cols><col min="1" max="1" width="27.7109375"

    customWidth="1"/><col min="2" max="2" width="19.5703125"

    customWidth="1"/><col min="3" max="3" width="19.42578125"

    customWidth="1"/></cols>
    <sheetData>
    <row r="1" spans="1:3"><c r="A1"

    s="1" t="s"><v>0</v></c><c r="B1" s="1" t="s"><v>1</v></c><c r="C1"

    s="1" t="s"><v>2</v></c></row>
    </sheetData>
    <pageMargins left="0.7"

    right="0.7" top="0.75" bottom="0.75" header="0.3"

    footer="0.3"/><pageSetup orientation="portrait" horizontalDpi="200"

    verticalDpi="200" copies="0" r:id="rId1"/></worksheet>

    second XML file is:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="11" uniqueCount="11">
    <si><r><t xml:space="preserve">How </t></r>
    <r><rPr><b/><sz val="11"/><color theme="1"/><rFont val="Calibri"/><family val="2"/><scheme val="minor"/></rPr><t>many</t></r>
    <r><rPr><sz val="11"/><color theme="1"/><rFont val="Calibri"/><family val="2"/><scheme val="minor"/></rPr><t xml:space="preserve"> </t></r>
    <r><rPr><u/><sz val="11"/><color theme="1"/><rFont val="Calibri"/><family val="2"/><scheme val="minor"/></rPr><t>sides</t></r>
    <r><rPr><sz val="11"/><color theme="1"/><rFont val="Calibri"/><family val="2"/><scheme val="minor"/></rPr><t xml:space="preserve"> in a </t></r>
    <r><rPr><b/><sz val="14"/><color rgb="FFFF0000"/><rFont val="Calibri"/><family val="2"/><scheme val="minor"/></rPr><t>triangle</t></r></si>
    <si><r><rPr><b/><sz val="11"/><color rgb="FFFF0000"/><rFont val="Calibri"/><family val="2"/><scheme val="minor"/></rPr><t>3</t></r><r><rPr><sz val="11"/><color theme="1"/><rFont val="Calibri"/><family val="2"/><scheme val="minor"/></rPr><t xml:space="preserve"> sides</t></r></si><si><r><t xml:space="preserve">These are </t></r><r><rPr><b/><u/><sz val="11"/><color rgb="FFFF0000"/><rFont val="Calibri"/><family val="2"/><scheme val="minor"/></rPr><t>comments</t></r></si></sst>

    Now i should get the <si> from second xml file with its format options as attributes and place this <si> tag in the <sheetdata><row><c> of the first xml file, so for each <si> in the second file, i should do the same thing.

    If anybody has the solution to this, or any suggestions to solve this issue, it would be of great help to me.

    Thanks All,

  • Re: Add the xml attirbutes from one xml file to the second xml file ?

    11-08-2009, 2:39 AM
    • All-Star
      25,597 point All-Star
    • qwe123kids
    • Member since 03-27-2008, 5:49 AM
    • Mumbai
    • Posts 4,372

    Hi,

    U may use Xmldocument

    http://www.stardeveloper.com/articles/display.html?article=2009031901&page=1

    http://www.aspnettutorials.com/tutorials/database/XML-Csharp.aspx

    http://www.aspfree.com/c/a/ASP.NET-Code/How-to-Load-XML-Documents-in-ASP-NET-2-0/

    OR

    U may Merge xml After Taking in dataset

    Thanks
    Avinash Tiwari

    Remember to click “Mark as Answer” on the post, if it helps you.

    MY Blog

    Hacking Inside .net exe
  • Re: Add the xml attirbutes from one xml file to the second xml file ?

    11-08-2009, 6:57 AM

    The first XML document sample is messed up so it is hard to recognize what you want to do with it. Also there seems to be more than one 'c' element, you have not explained to which one you want to copy the other elements. The following C# .NET 3.5 sample adds the 'si' elements to the first 'c' element:


                XDocument sheet1 = XDocument.Load(@"file1.xml");
                XDocument data = XDocument.Load(@"file2.xml");
    
                XNamespace m = sheet1.Root.Name.Namespace;
    
                sheet1.Root.Element(m + "b").Element(m + "sheetData").Element(m + "row").Element(m + "c").Add(
                    data.Root.Elements(m + "si")
                );
    
                sheet1.Save("result.xml");


    Martin Honnen --- MVP XML
    My blog
  • Re: Add the xml attirbutes from one xml file to the second xml file ?

    11-09-2009, 9:38 AM
    • Member
      30 point Member
    • dotnetmate
    • Member since 12-12-2007, 4:59 PM
    • Posts 130

    Martin,

    Thanks for the reply. I am posting the first XML file again here:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1:C4"/>
    <sheetViews><sheetView tabSelected="1" workbookViewId="0">
    <selection activeCell="A5" sqref="A5"/></sheetView></sheetViews>
    <sheetFormatPr defaultRowHeight="15"/><cols><col min="1" max="1" width="27.7109375" customWidth="1"/>
    <col min="2" max="2" width="19.5703125" customWidth="1"/>
    <col min="3" max="3" width="19.42578125" customWidth="1"/></cols>
    <sheetData><row r="1" spans="1:3">
    <c r="A1" s="1" t="s"><v>0</v></c>
    <c r="B1" s="1" t="s"><v>1</v></c>
    <c r="C1" s="1" t="s"><v>2</v></c></row>
    </sheetData><pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
    <pageSetup orientation="portrait" horizontalDpi="200" verticalDpi="200" copies="0" r:id="rId1"/>
    </worksheet>

    In this first XML file, the number of <si> elements will be same as number of <c> elemets in first xml file.

    so I should place the first <si> element from xml file2 into first <c> element of xml file1, and second <si> element from xml2 into second <c> element of xml1.

    Please let me know if I am not clear in my question.

    Thanks,
     

  • Re: Add the xml attirbutes from one xml file to the second xml file ?

    11-09-2009, 10:46 AM
    Answer

    The following code hopefully does what you want:

                XDocument sheet1 = XDocument.Load(@"xml1.xml");
                XDocument data = XDocument.Load(@"xml2.xml");
    
                XNamespace m = sheet1.Root.Name.Namespace;
    
                foreach (var item in sheet1.Root.Element(m + "b").Element(m + "sheetData").Element(m + "row").Elements(m + "c").Select((c, i) => new { c, i }))
                {
                    item.c.Add(data.Root.Elements(m + "si").ElementAt(item.i));
                }
    
                sheet1.Save("result.xml");
    


    Martin Honnen --- MVP XML
    My blog
Page 1 of 1 (5 items)