i have xml file , want split multiple xml files based in viewname.
per sample below, there 3 viewname values, , each, there viewpane elements , viewcolumn elements.
using linq, code far can save files encompass viewname nodes:
static void visualviews( xdocument xdoc) { var basefolder = prep(folderrds, "visualviews"); var viewnames = xdoc.element("visualmodelds").elements("viewname"); foreach (var viewname in viewnames) { var name = viewname.element("viewname").value; viewname.save(string.format(@"{0}{1}.xml", basefolder, name)); } }
i'm looking include other nodes have corresponding viewname value. think want group parent/child elements have same child viewname element, , save each group of elements own xml file, based on viewname element value.
the xml below parse 2 files, , i've embedded hard return differentiate elements go file.
so said, every parent element has child viewname element group by.
xml edited embed spaces element values
<?xml version="1.0" standalone="yes"?> <visualmodelds> <viewname> <viewname>a b c</viewname> </viewname> <viewpane> <viewname>a b c</viewname> <viewpane>alpha</viewpane> </viewpane> <viewcolumn> <viewname>a b c</viewname> <viewpane>alpha</viewpane> <viewcolumn>apples</viewcolumn> </viewcolumn> <viewcolumn> <viewname>a b c</viewname> <viewpane>alpha</viewpane> <viewcolumn>oranges</viewcolumn> </viewcolumn> <viewname> <viewname>x y z</viewname> </viewname> <viewpane> <viewname>x y z</viewname> <viewpane>cappa</viewpane> </viewpane> <viewcolumn> <viewname>x y z</viewname> <viewpane>cappa</viewpane> <viewcolumn>macadema</viewcolumn> </viewcolumn> <viewcolumn> <viewname>x y z</viewname> <viewpane>cappa</viewpane> <viewcolumn>pecan</viewcolumn> </viewcolumn> <viewpane> <viewname>x y z</viewname> <viewpane>omega</viewpane> </viewpane> <viewcolumn> <viewname>x y z</viewname> <viewpane>omega</viewpane> <viewcolumn>peanut</viewcolumn> </viewcolumn> </visualmodelds>
var viewnames = xdoc.root.elements("viewname").elements("viewname"); foreach (var viewname in viewnames) { var group = new xelement(viewname.value.replace(" ", ""), xdoc.root.elements().where(elem => elem.element("viewname").value == viewname.value)); group.save(viewname.value + ".txt"); }
Comments
Post a Comment