locked
XML Merging RRS feed

  • Question

  • Hello all,

    I have an XML document. The XML document has the following structure:

    ProductMaster
         ProductMasterHeader
         ProductMasterLine
              ProductCode
              ProductName


    Under the ProductMaster node , there is one ProductMasterHeader node and one or more
    ProductMasterLine node.

    Now, There is another text document, where each line is infact an ProductMasterLine.
    for e.g. The

    <ProductMasterLine><ProductMasterCode>P1</ProductMasterCode><ProductName>P1Name</ProductName></ProductMasterLine>
    <ProductMasterLine><ProductMasterCode>P2</ProductMasterCode><ProductName>P2Name</ProductName></ProductMasterLine>
    <ProductMasterLine><ProductMasterCode>P3</ProductMasterCode><ProductName>P3Name</ProductName></ProductMasterLine>
    <ProductMasterLine><ProductMasterCode>P4</ProductMasterCode><ProductName>P4Name</ProductName></ProductMasterLine>

    Now IN a C# program, how can i merge these ProductMasterLine from the text file into XML document above so that all ProductMasterLine
    come under the ProductMaster node?
    Any suggesstion please?

    regards,
    MS

    Friday, August 1, 2014 6:03 AM

Answers

  • Try this:

                XElement pm = new XElement
                ("ProductMaster",
                new XElement("ProductMasterHeader",  "P1"),
                new XElement("ProductMasterHeader",  "P2"),
                new XElement("ProductMasterHeader",  "P3"),
                new XElement("ProductMasterHeader",  "P4")
                );
                System.IO.StreamReader file =
                    new System.IO.StreamReader(@"D:\xmltest.txt");
                while ((line = file.ReadLine()) != null)
                {
                    XElement newline = XElement.Parse(line);
                    string pmNo = (string)newline.Descendants("ProductMasterCode").FirstOrDefault();
                    XElement parent = (XElement)pm.Descendants("ProductMasterHeader")
                        .Where(e => e.Value.ToString() == pmNo).FirstOrDefault();
                    parent.Add(newline);
                }
    
                pm.Save(@"D:\xmlresult.txt");

    That gives me:

    <?xml version="1.0" encoding="utf-8"?>
    <ProductMaster>
      <ProductMasterHeader>P1<ProductMasterLine><ProductMasterCode>P1</ProductMasterCode><ProductName>P1Name</ProductName></ProductMasterLine></ProductMasterHeader>
      <ProductMasterHeader>P2<ProductMasterLine><ProductMasterCode>P2</ProductMasterCode><ProductName>P2Name</ProductName></ProductMasterLine></ProductMasterHeader>
      <ProductMasterHeader>P3<ProductMasterLine><ProductMasterCode>P3</ProductMasterCode><ProductName>P3Name</ProductName></ProductMasterLine></ProductMasterHeader>
      <ProductMasterHeader>P4<ProductMasterLine><ProductMasterCode>P4</ProductMasterCode><ProductName>P4Name</ProductName></ProductMasterLine></ProductMasterHeader>
    </ProductMaster>


    • Edited by Andy ONeill Friday, August 1, 2014 7:55 AM
    • Proposed as answer by Ioana Vasilescu Friday, August 1, 2014 4:01 PM
    • Marked as answer by MS_Prog Sunday, August 3, 2014 11:40 PM
    Friday, August 1, 2014 7:55 AM

All replies

  • You can check this page for help:

    http://support.microsoft.com/kb/311530/en-us

    However because of different XML structure (ProductMasterHeader in base XML file) simple merging will not be enough.

    Friday, August 1, 2014 7:37 AM
  • Try this:

                XElement pm = new XElement
                ("ProductMaster",
                new XElement("ProductMasterHeader",  "P1"),
                new XElement("ProductMasterHeader",  "P2"),
                new XElement("ProductMasterHeader",  "P3"),
                new XElement("ProductMasterHeader",  "P4")
                );
                System.IO.StreamReader file =
                    new System.IO.StreamReader(@"D:\xmltest.txt");
                while ((line = file.ReadLine()) != null)
                {
                    XElement newline = XElement.Parse(line);
                    string pmNo = (string)newline.Descendants("ProductMasterCode").FirstOrDefault();
                    XElement parent = (XElement)pm.Descendants("ProductMasterHeader")
                        .Where(e => e.Value.ToString() == pmNo).FirstOrDefault();
                    parent.Add(newline);
                }
    
                pm.Save(@"D:\xmlresult.txt");

    That gives me:

    <?xml version="1.0" encoding="utf-8"?>
    <ProductMaster>
      <ProductMasterHeader>P1<ProductMasterLine><ProductMasterCode>P1</ProductMasterCode><ProductName>P1Name</ProductName></ProductMasterLine></ProductMasterHeader>
      <ProductMasterHeader>P2<ProductMasterLine><ProductMasterCode>P2</ProductMasterCode><ProductName>P2Name</ProductName></ProductMasterLine></ProductMasterHeader>
      <ProductMasterHeader>P3<ProductMasterLine><ProductMasterCode>P3</ProductMasterCode><ProductName>P3Name</ProductName></ProductMasterLine></ProductMasterHeader>
      <ProductMasterHeader>P4<ProductMasterLine><ProductMasterCode>P4</ProductMasterCode><ProductName>P4Name</ProductName></ProductMasterLine></ProductMasterHeader>
    </ProductMaster>


    • Edited by Andy ONeill Friday, August 1, 2014 7:55 AM
    • Proposed as answer by Ioana Vasilescu Friday, August 1, 2014 4:01 PM
    • Marked as answer by MS_Prog Sunday, August 3, 2014 11:40 PM
    Friday, August 1, 2014 7:55 AM
  • Just a note: You should not trust a text file. Thus you should validate the result of your merge against a schema.
    Friday, August 1, 2014 8:15 AM