c# - XML Parsing - Parsing 2 identical named XMLArrays -
i've searched arround did not find solution fit requiremnts. i’m trying parse information of the .csproj file c# object.
i'm using function this. method gives posibilbity parse xmlfile long have representation of file c# object cast it.
public static object deserializexml(string xmlpath) { if (file.exists(xmlpath)) { object returnobject; xmlserializer serializer = new xmlserializer(typeof (projectconfiguration)); using (filestream fs = new filestream(xmlpath, filemode.open)) { xmlreader reader = xmlreader.create(fs); returnobject = serializer.deserialize(reader); fs.close(); } return returnobject; } return null; }
here problem. csproj file shows similar xmlarrays.
<itemgroup> <reference include="system" /> <reference include="system.core" /> </itemgroup> <itemgroup> <compile include="class1.cs" /> <compile include="properties\assemblyinfo.cs" /> </itemgroup>enter code here
does of have idea how annotated in c# class file ? throws error...
[xmlarray("itemgroup")] [xmlarrayitem("compile")] [xmlattribute("include")] public list<string> itemgroupcompileincludesurrogate { {...} set {...} } [xmlarray("itemgroup")] [xmlarrayitem("reference ")] [xmlattribute("include")] public list<string> itemgroupcompileincludesurrogate { {...} set {...} }
thanks of in forcast ! ;-)
[edit] after using first idea -> :-) looks this
<itemgroup> <reference include="system" /> <reference include="system.core" /> <compile include="class1.cs" /> <compile include="properties\assemblyinfo.cs" /> </itemgroup>
greez iki
try this
using system; using system.collections.generic; using system.linq; using system.text; using system.xml; using system.xml.serialization; using system.io; namespace consoleapplication1 { class program { const string filename = @"c:\temp\test.xml"; static void main(string[] args) { root root = new root() { itemgroup = new list<itemgroup>() { new itemgroup(){ reference = new list<reference>() { new reference(){ include = "system"}, new reference(){ include= "system.core"} } }, new itemgroup(){ compile = new list<compile>(){ new compile() { include = "class1.cs"}, new compile() { include = "properties\\assemblyinfo.cs"} } } } }; xmlserializer serializer = new xmlserializer(typeof(root)); streamwriter writer = new streamwriter(filename); serializer.serialize(writer, root); writer.flush(); writer.close(); writer.dispose(); xmlserializer xs = new xmlserializer(typeof(root)); xmltextreader reader = new xmltextreader(filename); root newroot = (root)xs.deserialize(reader); } } [xmlroot("root")] public class root { [xmlelement("itemgroup")] public list<itemgroup> itemgroup { get; set; } } [xmlroot("itemgroup")] public class itemgroup { [xmlelement("compile")] public list<compile> compile { get; set; } [xmlelement("reference")] public list<reference> reference { get; set; } } [xmlroot("compile")] public class compile { [xmlattribute("include")] public string include { get; set; } } [xmlroot("reference")] public class reference { [xmlattribute("include")] public string include { get; set; } } }
Comments
Post a Comment