Ask a questionAsk a question
 

AnswerReorder elements

  • Thursday, March 20, 2008 5:57 AMPunu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    private const string displayXml = @"<?xml version=""1.0"" encoding=""UTF-8""?><Displays><Display>Address1</Display><Display>TheatreName</Display><Display>Address2</Display><Display>City</Display></Displays>";

     

     

    string result = @"<NewDataSet>
      <SearchResults>
        <TheatreID>3528</TheatreID>
        <TheatreName>AMC OAK PARK PLAZA 6</TheatreName>
        <Address1>9747 QUIVIRA RD</Address1>
        <City>SHAWNEE MISSION</City>
        <StateAbbr>KS</StateAbbr>
        <ZipPlus4>66215</ZipPlus4>
        <InterceptMarket>SHAWNEE MISSION</InterceptMarket>
      </SearchResults>
      <SearchResults>
        <TheatreID>5135</TheatreID>
        <TheatreName>AMC OAK TREE 6</TheatreName>
        <Address1>10006 AURORA AVE N</Address1>
        <City>SEATTLE</City>
        <StateAbbr>WA</StateAbbr>
        <ZipPlus4>98133</ZipPlus4>
        <InterceptMarket>SEATTLE</InterceptMarket>
      </SearchResults>
      <SearchResults>
        <TheatreID>7821</TheatreID>
        <TheatreName>AMC OAKVIEW 24</TheatreName>
        <Address1>3555 S 140TH PLZ</Address1>
        <City>OMAHA</City>
        <StateAbbr>NE</StateAbbr>
        <ZipPlus4>68144</ZipPlus4>
        <InterceptMarket>OMAHA</InterceptMarket>
      </SearchResults>
    </NewDataSet>"

     

     

    How can I redorder my elements in my result Xml based on my display Xml (so my address1 comes before theathrename....) . Also I need to to add missing elements from display Xml into result Xml.

     

    Any help is appreciated

    Thanks

    Punu

Answers

  • Thursday, March 20, 2008 6:35 PMIon Vasilian Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Here's how you can do it in-place:

     

    Code Snippet

        static void Main() {

            string format = @"

    <Displays>

        <Display>Address1</Display>

        <Display>TheatreName</Display>

        <Display>Address2</Display>

        <Display>City</Display>

    </Displays>";

            string result = @"

    <NewDataSet>

      <SearchResults>

        <TheatreID>3528</TheatreID>

        <TheatreName>AMC OAK PARK PLAZA 6</TheatreName>

        <Address1>9747 QUIVIRA RD</Address1>

        <City>SHAWNEE MISSION</City>

        <StateAbbr>KS</StateAbbr>

        <ZipPlus4>66215</ZipPlus4>

        <InterceptMarket>SHAWNEE MISSION</InterceptMarket>

      </SearchResults>

      <SearchResults>

        <TheatreID>5135</TheatreID>

        <TheatreName>AMC OAK TREE 6</TheatreName>

        <Address1>10006 AURORA AVE N</Address1>

        <City>SEATTLE</City>

        <StateAbbr>WA</StateAbbr>

        <ZipPlus4>98133</ZipPlus4>

        <InterceptMarket>SEATTLE</InterceptMarket>

      </SearchResults>

      <SearchResults>

        <TheatreID>7821</TheatreID>

        <TheatreName>AMC OAKVIEW 24</TheatreName>

        <Address1>3555 S 140TH PLZ</Address1>

        <City>OMAHA</City>

        <StateAbbr>NE</StateAbbr>

        <ZipPlus4>68144</ZipPlus4>

        <InterceptMarket>OMAHA</InterceptMarket>

      </SearchResults>

    </NewDataSet>";

            XElement displays = XElement.Parse(format);

            XElement newDataSet = XElement.Parse(result);

            foreach (XElement searchResults in newDataSet.Elements("SearchResults")) {

                searchResults.ReplaceAll(

                    // re-order according to the display instructions

                    from display in displays.Elements("Display")

                    select searchResults.Element((string)display),

                    // add the missing elements 

                    from searchResult in searchResults.Elements()

                    where !displays.Elements("Display").Select(e => (string)e).Contains(searchResult.Name.ToString())

                    select searchResult

                );

            }

            Console.WriteLine(newDataSet);

        }

     

     

     

    Ion
  • Friday, March 21, 2008 9:09 AMMatthieuMEZIL Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Ion is right but if you don't want the foreach loop, you can also do this:

    Code Snippet

    class Program

    {

        private const string displayXml = @"<?xml version=""1.0"" encoding=""UTF-8""?><Displays><Display>Address1</Display><Display>TheatreName</Display><Display>Address2</Display><Display>City</Display></Displays>";

     

        static void Main(string[] args)

        {

            string result = @"<NewDataSet>

                            <SearchResults>

                              <TheatreID>3528</TheatreID>

                              <TheatreName>AMC OAK PARK PLAZA 6</TheatreName>

                              <Address1>9747 QUIVIRA RD</Address1>

                              <City>SHAWNEE MISSION</City>

                              <StateAbbr>KS</StateAbbr>

                              <ZipPlus4>66215</ZipPlus4>

                              <InterceptMarket>SHAWNEE MISSION</InterceptMarket>

                            </SearchResults>

                            <SearchResults>

                              <TheatreID>5135</TheatreID>

                              <TheatreName>AMC OAK TREE 6</TheatreName>

                              <Address1>10006 AURORA AVE N</Address1>

                              <City>SEATTLE</City>

                              <StateAbbr>WA</StateAbbr>

                              <ZipPlus4>98133</ZipPlus4>

                              <InterceptMarket>SEATTLE</InterceptMarket>

                            </SearchResults>

                            <SearchResults>

                              <TheatreID>7821</TheatreID>

                              <TheatreName>AMC OAKVIEW 24</TheatreName>

                              <Address1>3555 S 140TH PLZ</Address1>

                              <City>OMAHA</City>

                              <StateAbbr>NE</StateAbbr>

                              <ZipPlus4>68144</ZipPlus4>

                              <InterceptMarket>OMAHA</InterceptMarket>

                            </SearchResults>

                          </NewDataSet>";

     

            var displayXmlElement = XElement.Parse(displayXml);

            var resultXmlElement = XElement.Parse(result);

     

            var newXml = new XElement("NewDataSet",

                from searchResult in resultXmlElement.Descendants("SearchResults")

                select new XElement("SearchResults",

                    (from display in displayXmlElement.Descendants("Display")

                    select searchResult.Element(display.Value)).Union(

                    from searchResultElement in searchResult.Elements().Select(sre => sre.Name.LocalName)

                    where !displayXmlElement.Descendants("Display").Select(dxe => dxe.Value).Contains(searchResultElement)

                    select searchResult.Element(searchResultElement))));

        }

    }

     

     

     

All Replies

  • Thursday, March 20, 2008 8:48 AMMatthieuMEZIL Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    You can do this:

    Code Snippet

    class Program

    {

        private const string displayXml = @"<?xml version=""1.0"" encoding=""UTF-8""?><Displays><Display>Address1</Display><Display>TheatreName</Display><Display>Address2</Display><Display>City</Display></Displays>";

     

        static void Main(string[] args)

        {

            string result = @"<NewDataSet>

                                <SearchResults>

                                  <TheatreID>3528</TheatreID>

                                  <TheatreName>AMC OAK PARK PLAZA 6</TheatreName>

                                  <Address1>9747 QUIVIRA RD</Address1>

                                  <City>SHAWNEE MISSION</City>

                                  <StateAbbr>KS</StateAbbr>

                                  <ZipPlus4>66215</ZipPlus4>

                                  <InterceptMarket>SHAWNEE MISSION</InterceptMarket>

                                </SearchResults>

                                <SearchResults>

                                  <TheatreID>5135</TheatreID>

                                  <TheatreName>AMC OAK TREE 6</TheatreName>

                                  <Address1>10006 AURORA AVE N</Address1>

                                  <City>SEATTLE</City>

                                  <StateAbbr>WA</StateAbbr>

                                  <ZipPlus4>98133</ZipPlus4>

                                  <InterceptMarket>SEATTLE</InterceptMarket>

                                </SearchResults>

                                <SearchResults>

                                  <TheatreID>7821</TheatreID>

                                  <TheatreName>AMC OAKVIEW 24</TheatreName>

                                  <Address1>3555 S 140TH PLZ</Address1>

                                  <City>OMAHA</City>

                                  <StateAbbr>NE</StateAbbr>

                                  <ZipPlus4>68144</ZipPlus4>

                                  <InterceptMarket>OMAHA</InterceptMarket>

                                </SearchResults>

                              </NewDataSet>";

     

            var displayXmlElement = XElement.Parse(displayXml);

            var resultXmlElement = XElement.Parse(result);

     

            var newXml = new XElement("NewDataSet",

                from searchResult in resultXmlElement.Descendants("SearchResults")

                select new XElement("SearchResults",

                    from display in displayXmlElement.Descendants("Display")

                    select searchResult.Element(display.Value)));

        }

    }

     

     

    If you want a string, you can simply call ToString() method on newXml.

  • Thursday, March 20, 2008 6:25 PMPunu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     


    Hi,

    Thanks a lot for the input, this helps, but this does not add the missing element in result Xml to the newXml.

    Like DisplayXml has Address2, while ResultXml does not have that. I want new xml to have all the elements from displayXml.

    Thanks
    Punu
  • Thursday, March 20, 2008 6:35 PMIon Vasilian Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Here's how you can do it in-place:

     

    Code Snippet

        static void Main() {

            string format = @"

    <Displays>

        <Display>Address1</Display>

        <Display>TheatreName</Display>

        <Display>Address2</Display>

        <Display>City</Display>

    </Displays>";

            string result = @"

    <NewDataSet>

      <SearchResults>

        <TheatreID>3528</TheatreID>

        <TheatreName>AMC OAK PARK PLAZA 6</TheatreName>

        <Address1>9747 QUIVIRA RD</Address1>

        <City>SHAWNEE MISSION</City>

        <StateAbbr>KS</StateAbbr>

        <ZipPlus4>66215</ZipPlus4>

        <InterceptMarket>SHAWNEE MISSION</InterceptMarket>

      </SearchResults>

      <SearchResults>

        <TheatreID>5135</TheatreID>

        <TheatreName>AMC OAK TREE 6</TheatreName>

        <Address1>10006 AURORA AVE N</Address1>

        <City>SEATTLE</City>

        <StateAbbr>WA</StateAbbr>

        <ZipPlus4>98133</ZipPlus4>

        <InterceptMarket>SEATTLE</InterceptMarket>

      </SearchResults>

      <SearchResults>

        <TheatreID>7821</TheatreID>

        <TheatreName>AMC OAKVIEW 24</TheatreName>

        <Address1>3555 S 140TH PLZ</Address1>

        <City>OMAHA</City>

        <StateAbbr>NE</StateAbbr>

        <ZipPlus4>68144</ZipPlus4>

        <InterceptMarket>OMAHA</InterceptMarket>

      </SearchResults>

    </NewDataSet>";

            XElement displays = XElement.Parse(format);

            XElement newDataSet = XElement.Parse(result);

            foreach (XElement searchResults in newDataSet.Elements("SearchResults")) {

                searchResults.ReplaceAll(

                    // re-order according to the display instructions

                    from display in displays.Elements("Display")

                    select searchResults.Element((string)display),

                    // add the missing elements 

                    from searchResult in searchResults.Elements()

                    where !displays.Elements("Display").Select(e => (string)e).Contains(searchResult.Name.ToString())

                    select searchResult

                );

            }

            Console.WriteLine(newDataSet);

        }

     

     

     

    Ion
  • Friday, March 21, 2008 9:09 AMMatthieuMEZIL Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Ion is right but if you don't want the foreach loop, you can also do this:

    Code Snippet

    class Program

    {

        private const string displayXml = @"<?xml version=""1.0"" encoding=""UTF-8""?><Displays><Display>Address1</Display><Display>TheatreName</Display><Display>Address2</Display><Display>City</Display></Displays>";

     

        static void Main(string[] args)

        {

            string result = @"<NewDataSet>

                            <SearchResults>

                              <TheatreID>3528</TheatreID>

                              <TheatreName>AMC OAK PARK PLAZA 6</TheatreName>

                              <Address1>9747 QUIVIRA RD</Address1>

                              <City>SHAWNEE MISSION</City>

                              <StateAbbr>KS</StateAbbr>

                              <ZipPlus4>66215</ZipPlus4>

                              <InterceptMarket>SHAWNEE MISSION</InterceptMarket>

                            </SearchResults>

                            <SearchResults>

                              <TheatreID>5135</TheatreID>

                              <TheatreName>AMC OAK TREE 6</TheatreName>

                              <Address1>10006 AURORA AVE N</Address1>

                              <City>SEATTLE</City>

                              <StateAbbr>WA</StateAbbr>

                              <ZipPlus4>98133</ZipPlus4>

                              <InterceptMarket>SEATTLE</InterceptMarket>

                            </SearchResults>

                            <SearchResults>

                              <TheatreID>7821</TheatreID>

                              <TheatreName>AMC OAKVIEW 24</TheatreName>

                              <Address1>3555 S 140TH PLZ</Address1>

                              <City>OMAHA</City>

                              <StateAbbr>NE</StateAbbr>

                              <ZipPlus4>68144</ZipPlus4>

                              <InterceptMarket>OMAHA</InterceptMarket>

                            </SearchResults>

                          </NewDataSet>";

     

            var displayXmlElement = XElement.Parse(displayXml);

            var resultXmlElement = XElement.Parse(result);

     

            var newXml = new XElement("NewDataSet",

                from searchResult in resultXmlElement.Descendants("SearchResults")

                select new XElement("SearchResults",

                    (from display in displayXmlElement.Descendants("Display")

                    select searchResult.Element(display.Value)).Union(

                    from searchResultElement in searchResult.Elements().Select(sre => sre.Name.LocalName)

                    where !displayXmlElement.Descendants("Display").Select(dxe => dxe.Value).Contains(searchResultElement)

                    select searchResult.Element(searchResultElement))));

        }

    }

     

     

     

  • Friday, March 21, 2008 9:19 AMMatthieuMEZIL Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    You can also do it without Union:

    Code Snippet

    var newXml = new XElement("NewDataSet",

        from searchResult in resultXmlElement.Descendants("SearchResults")

        select new XElement("SearchResults",

            (from display in displayXmlElement.Descendants("Display")

            select searchResult.Element(display.Value)),

            from searchResultElement in searchResult.Elements().Select(sre => sre.Name.LocalName)

            where !displayXmlElement.Descendants("Display").Select(dxe => dxe.Value).Contains(searchResultElement)

            select searchResult.Element(searchResultElement)));

     

     

    And more funny and shorter, like this:

    Code Snippet

    var newXml = new XElement("NewDataSet",

        from searchResult in resultXmlElement.Descendants("SearchResults")

        select new XElement("SearchResults",

            (from display in displayXmlElement.Descendants("Display")

            select searchResult.Element(display.Value)).Union(searchResult.Elements()).Distinct()));

     

     

    Enjoy