views:

82

answers:

2

I've ran into a problem and wondered if there's simple way of solving it.

Here I have a XML template, defining some properties and their values.

<Properties>
  <Property name="ID">10000</Property>
  <Property name="Name">
    <SubProperty name="FirstName">Foo</SubProperty>
    <SubProperty name="LastName">Bar</SubProperty >
  </Property>
</Properties>

All I what is to extract the Properties/subProperties defined in the template to generate a new XML file, with all values attached, something like

<Items>
  <ID>10000</ID>
  <Name>
    <FirstName>Foo</FirstName>
    <LastName>Bar</LastName>
  </Name>
</Items>

Since I don't know the content of the template in design time, I tried to load it and created a List class use LINQ, but cannot get the result above when serialize it directly. So instead of create a List class I created a dynamic object using Reflection.Emit and then serialize the object to XML.


private static readonly XDocument doc = XDocument.Load("Template.xml");

static void Main(string[] args) {
    var newType = CreateDynamicType();
    var newObject = Activator.CreateInstance(newType);
    var properties = newType.GetProperties();
    foreach (var property in properties) {
        // assign values
    }
    SerializeToXml(newObject);
}

private static Type CreateDynamicType() {

    AssemblyName assemblyName = new AssemblyName() { Name = "DynamicTypeAdapter" };
    AssemblyBuilder assembly = 
        Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
    ModuleBuilder module = 
        assembly.DefineDynamicModule(assembly.GetName().Name, false);
    TypeBuilder type = module.DefineType("Items", TypeAttributes.Public | TypeAttributes.Class);

    foreach (var p in doc.Descendants("Property")) {
        string pName = p.Attribute("name").Value;
        TypeBuilder subType = module.DefineType(pName, TypeAttributes.Public | TypeAttributes.Class);
        foreach (var sp in p.Descendants("SubProperty")) {
            CreateDynamicProperty(subType, sp.Attribute("name").Value, typeof(string));
        }
        var propertyType = subType.CreateType();
        CreateDynamicProperty(type, pName, propertyType);
    }

    return type.CreateType();
}

private static void CreateDynamicProperty(TypeBuilder typeBuilder, string propertyName, Type propertyType) {
    PropertyBuilder property = typeBuilder.DefineProperty(propertyName,
    PropertyAttributes.None, propertyType, new Type[] { typeof(string) });

    FieldBuilder field = typeBuilder.DefineField("_" + propertyName, propertyType, FieldAttributes.Private);

    MethodAttributes GetSetAttributes = MethodAttributes.Public | MethodAttributes.HideBySig;

    MethodBuilder getMethod =
        typeBuilder.DefineMethod("get_value", GetSetAttributes, propertyType, Type.EmptyTypes);

    ILGenerator getIL = getMethod.GetILGenerator();
    getIL.Emit(OpCodes.Ldarg_0);
    getIL.Emit(OpCodes.Ldfld, field);
    getIL.Emit(OpCodes.Ret);

    MethodBuilder setMethod =
            typeBuilder.DefineMethod("set_value", GetSetAttributes, null, new Type[] { typeof(string) });

    ILGenerator setIL = setMethod.GetILGenerator();
    setIL.Emit(OpCodes.Ldarg_0);
    setIL.Emit(OpCodes.Ldarg_1);
    setIL.Emit(OpCodes.Stfld, field);
    setIL.Emit(OpCodes.Ret);

    property.SetGetMethod(getMethod);
    property.SetSetMethod(setMethod);
}

It works fine but is there any simple way of doing this? Any comment is appreciated. Thanks

+1  A: 

Based on what you have above, something simple like this should work.

var root = XElement.Parse(xml);
var result = new XElement("Items");
foreach (var p in root.Descendants("Property")) 
{
 var subs =  p.Descendants("SubProperty").Select( sp => Transpose(sp) );

    // The trick is here - XElement constructor uses params object[], 
    // so we can pass an arbitrary number of arguments to build the XElement
 var item = new XElement( p.Attribute("name").Value, subs, subs.Any() : null ? p.Value );

    result.Add( item );
}

// Transpose method
XElement Transpose(XElement xe)
{
 return new XElement( xe.Attribute("name").Value, xe.Value );
}


// result
<Items>
  <ID>10000</ID>
  <Name>
    <FirstName>Foo</FirstName>
    <LastName>Bar</LastName>
  </Name>
</Items>

NOTE: If you have multiple levels of nesting, or need to be able to differentiate multiple Item nodes within the template, you'll need to think about it some more.

Winston Smith
Thanks Winston, great help
Gnavvy
+1  A: 

If all you want to do change (transform) one XML format to another XML format then I think the approach you are taking is not the most appropriate. There are other APIs in the framework that support this kind of functionality. In your case the requirement seems rather simple, so I would opt for the Linq To Xml option. The following is a quick example, which produces the desired output.

XDocument doc = XDocument.Parse(@"<Properties>
                <Property name='ID'>10000</Property>
                <Property name='Name'>
                    <SubProperty name='FirstName'>Foo</SubProperty>
                    <SubProperty name='LastName'>Bar</SubProperty>
                </Property>
            </Properties>");

XElement items = new XElement("Items", 
                               from property in doc.Descendants("Property")
                               select new XElement((string)property.Attribute("name"),
                                                    // If there are no child elements (SubPropety)
                                                    // get the property value
                                                    property.HasElements ? null : (string)property,
                                                    // Another way for checking if there are any child elements
                                                    // You could also use property.HasElements like the previous statement
                                                    property.Elements("SubProperty").Any() ?
                                                    from subproperty in property.Elements("SubProperty")
                                                    select new XElement((string)subproperty.Attribute("name"),
                                                                        (string)subproperty) : null)

                          );

A few resouces that may be of help include:

http://msdn.microsoft.com/en-us/library/bb387098.aspx

http://msdn.microsoft.com/en-us/library/bb308960.aspx

http://iqueryable.com/2007/08/03/TransformingXMLWithLINQToXML.aspx

http://www.codeproject.com/KB/linq/LINQtoXML.aspx

Garett
Thanks a lot for the solution.
Gnavvy