Sunday, 15 May 2011

C#: Creating XML document using Linq having not proper output? -



C#: Creating XML document using Linq having not proper output? -

i'm trying create xml info data, creation successfull output bad how can prepare issue?

here code:

private void btngenerate_click(object sender, eventargs e) { xelement xml = new xelement("navigation", new xelement("navigationsets")); foreach (datarow row_navs in getnavigationsets().rows) { xml.add(new xelement("navigationname", row_navs["name"].tostring())); foreach (datarow row_sets in getmenusinnavigationsetbynavigation(2).rows) { if (int.parse(row_sets["id"].tostring()) == int.parse(row_navs["id"].tostring())) { foreach (datarow row_menus in getmenubyid(int.parse(row_sets["menu_id"].tostring())).rows) { xml.add(new xelement("menuname", row_menus["name"].tostring())); } } } } xml.save("data.xml"); }

im expecting output this

<?xml version="1.0" encoding="utf-8"?> <navigation> <navigationsets> <navigationname> <menuname></menuname> </navigationname> <navigationsets/> </navigation>

in current code output this

<?xml version="1.0" encoding="utf-8"?> <navigation> <navigationsets/> <navigationname></navigationname> <menuname></menuname> </navigation>

look @ when you're adding elements:

xml.add(new xelement("navigationname", row_navs["name"].tostring())); xml.add(new xelement("menuname", row_menus["name"].tostring()));

where xml element:

xelement xml = new xelement("navigation", new xelement("navigationsets"));

that means xml navigation element, not navigationsets element. suspect want like:

xelement outer = new xelement("navigation"); xelement inner = new xelement("navigationsets"); outer.add(inner);

... add together inner.

c# xml linq

No comments:

Post a Comment