score:2
Accepted answer
i don't know if there are any build-in functions to flaten the xml, but i'm very concerned they can fit your needs, because some attributes must be ignored according to your example.
you better use linq to xml which allows transforming such documents very easy and with not a lot of code. see my solution:
string origxml = "<rates id=\"1\" rateseffectivatedate=\"27/02/2014\">\n <type name=\"type1\" code=\"a\">\n <del id=\"d1\">\n <field1>10</field1>\n <field2>20</field2>\n <field3>30</field3>\n <field4>40</field4>\n </del>\n <del id=\"d2\">\n <field1>50</field1>\n <field2>60</field2>\n <field3>70</field3>\n <field4>80</field4>\n </del>\n </type>\n <type name=\"type2\" code=\"b\">\n <del id=\"d1\">\n <field1>110</field1>\n <field2>120</field2>\n <field3>130</field3>\n <field4>140</field4>\n </del>\n <del id=\"d3\">\n <field1>150</field1>\n <field2>160</field2>\n <field3>170</field3>\n <field4>180</field4>\n </del>\n </type>\n</rates>";
var xdoc = xdocument.parse(origxml);
var resdoc = new xdocument(
new xelement("rates",
xdoc.element("rates")
.elements("type")
.selectmany(typeel =>
typeel.elements("del")
.selectmany(delel =>
delel.elements()
.select(fieldel =>
new xelement("rate",
new xelement("id", typeel.attribute("name").value),
new xelement("code", typeel.attribute("code").value),
new xelement("delid", delel.attribute("id").value),
new xelement(fieldel.name, fieldel.value)))))
));
resdoc.save("transformeddoc.xml", saveoptions.none);
the output is exactly what you provided for your example.
score:2
you can do it like this:
var xmldocument = xdocument.load("path");
var rates = new list<xelement>();
foreach (var type in xmldocument.descendants("type"))
{
foreach (var del in type.elements("del"))
{
foreach (var field in del.elements())
{
xelement rate = new xelement("rate",
new xelement("id", (string) type.attribute("name")),
new xelement("code", (string) type.attribute("code")),
new xelement("delid", (string) del.attribute("id")),
new xelement(field.name, (string) field));
rates.add(rate);
}
}
}
xelement root = new xelement("rates");
root.add(rates);
root.save("newfile.xml");
another way using linq
instead of loops but i think this is less readable
var xmldocument = xdocument.load("path");
var newxml = new xelement("rates",
xmldocument.descendants()
.where(x => x.name.tostring().startswith("field"))
.select(
x =>
new xelement("rate",
new xelement("id", (string) x.parent.parent.attribute("name")),
new xelement("code", (string) x.parent.parent.attribute("code")),
new xelement("delid", (string) x.parent.attribute("id")),
new xelement(x.name, (string) x))));
newxml.save("newfile.xml");
Source: stackoverflow.com
Related Query
- How to flatten a multi level XML into a single level XML using c# code LINQ
- How to load xml code block into existing xml file at a specific node using linq
- how to add/insert conditional node into XML using linq to XML
- How could I create single list from Parent and child node from XML using Linq
- how do I parse xml into a tree of objects using linq only?
- How to get the name of elements/attributes in xml files and write these into another new xml file using LINQ
- How to get a count of every single element in an xml file using linq to xml?
- How to rewrite several independent LINQ queries into single one using Aggregate()?
- How to assign two different entities result set into single var variable using linq to sql c#
- How to load XML Elements using LINQ from XDocument into a class (not using Descendants)
- How to select all parent objects into DataContext using single LINQ query?
- How to get elements by name in XML using LINQ
- How do I insert an element into XML using Linq?
- How to properly search xml document using LINQ C#
- How to combine multiple fields into one field using LINQ
- How do I combine these two linq queries into a single query?
- LINQ Query - How to map a resultset into another object using Select
- How do you flatten a Linq query after using double grouping?
- How can I use LINQ to project this parent and children object model into a flat, single object?
- How do I group by Events by year using a single LINQ query?
- How to reuse a linq expression for 'Where' when using multiple source tables
- how to compare 2 XML using LINQ in C#
- How to update a single item of liist of objects using LINQ in C#
- How to read XML file using System.IO.Stream with LINQ
- How to check if XML contains element when using LINQ to XML?
- How can I do a multi level parent-child sort using Linq?
- How do I read/write an Encrypted XML file using LINQ to XML?
- How can I combine this code into one or two LINQ queries?
- How to sort datatable with multi column using LINQ
- Using LINQ to parse XML into Dictionary
More Query from same tag
- Linq insert with nullable datetime column
- Get grouped comma separated values with linq
- Entity Framework 6, Oracle - Linq Join Query generates unwanted where condition
- What will be LINQ MVC query equivalent to given SQL query?
- Formatting Linq Outputs
- Cannot use list of classes by base interface
- Linq on string array
- LINQ - How can I sort this data in one query
- LINQ to XML getting XElement values
- C# SQL to Linq - translation
- Using case statement in linq query
- Linq to Entities - where statement throws System.NotSupported Exception
- Linq - Inner join with group with sum
- Doing an 'OrderByDescending' on each child List in a Parent List
- Best searchable collection strategy for performance?
- Intersecting any number of Lists
- Display rows in MVC .NET from joining tables
- How can i fix this error? c# asp linq
- How to use linq to paginate a SortedList in Ascending or Descending order
- How to perform 'AND' operation in two lists in c# LINQ?
- C# foreach to linq
- How can I restrict a LINQ Query?
- Merging multiple List<CustomType> when grouping in LINQ
- Linq to Xml Convert a list
- How to group a list of lists using LINQ (ex: List<List<int>>)
- LINQ to Entities query with join inside method for use in MVC app
- How to join tables using linq and entity framework
- Extract key value from a string Using Linq
- Linq check value in SQL table always is false
- Converting ORDER BY with multiple case statements