Reorder elements
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
Here's how you can do it in-place:
Code Snippetstatic 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);
}
IonIon is right but if you don't want the foreach loop, you can also do this:
Code Snippetclass 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
You can do this:
Code Snippetclass 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.
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
PunuHere's how you can do it in-place:
Code Snippetstatic 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);
}
IonIon is right but if you don't want the foreach loop, you can also do this:
Code Snippetclass 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))));
}
}
You can also do it without Union:
Code Snippetvar 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 Snippetvar 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

