Reputation: 420
i've searched actually arround but did not find a solution to fit my requiremnts. What I’m trying to do is to parse some information of the the .csproj file into a c# object.
I'm using this function to do this. The method gives the posibilbity to parse any xmlFile as long as i have a representation of the file as a c# object to 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 is the problem. The csproj file shows to 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 any of have a idea how to annotated that in the c# class file ? This throws an error...
[XmlArray("ItemGroup")]
[XmlArrayItem("Compile")]
[XmlAttribute("Include")]
public List<string> ItemGroupCompileIncludeSurrogate
{
get {...}
set {...}
}
[XmlArray("ItemGroup")]
[XmlArrayItem("Reference ")]
[XmlAttribute("Include")]
public List<string> ItemGroupCompileIncludeSurrogate
{
get {...}
set {...}
}
Thanks all of you in forcast ! ;-)
[EDIT] After using the first idea -> Thanks :-) it looks like this
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Compile Include="Class1.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Greez Iki
Upvotes: 0
Views: 57
Reputation: 34421
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; }
}
}
Upvotes: 1
Reputation: 724
try:
public class Root
{
[XmlElement("ItemGroup")]
public List<ItemGroup> ItemGroups { get; set; }
}
public class ItemGroup
{
[XmlElement("Reference")]
public List<ReferenceOrCompile> References { get; set; }
[XmlElement("Compile")]
public List<ReferenceOrCompile> Compiles { get; set; }
}
public class ReferenceOrCompile
{
[XmlAttribute("Include")]
public string Include { get; set; }
}
if you want object convert to xml, use:
var item1 = new ReferenceOrCompile() { Include = "System" };
var item2 = new ReferenceOrCompile() { Include = "System.Core" };
var item3 = new ReferenceOrCompile() { Include = "Class1.cs" };
var item4 = new ReferenceOrCompile() { Include = "Properties\\AssemblyInfo.cs" };
var group1 = new ItemGroup()
{
References = new List<ReferenceOrCompile>()
{
item1, item2
}
};
var group2 = new ItemGroup()
{
Compiles = new List<ReferenceOrCompile>()
{
item3, item4
}
};
var root = new Root()
{
ItemGroups = new List<ItemGroup>()
{
group1, group2
}
}
then Serialize(root)
Upvotes: 1