var xml_helpers = require('./xml-helpers'),
    et = require('elementtree'),
    fs = require('fs');

function csproj(location) {
    this.location = location;
    this.xml = xml_helpers.parseElementtreeSync(location);
    return this;
}

csproj.prototype = {
    write:function() {
        fs.writeFileSync(this.location, this.xml.write({indent:4}), 'utf-8');
    },
    addSourceFile:function(relative_path) {
        relative_path = relative_path.split('/').join('\\');
        var item = new et.Element('ItemGroup');
        var compile = new et.Element('Compile');
        compile.attrib.Include = relative_path;
        item.append(compile);
        this.xml.getroot().append(item);
    },
    removeSourceFile:function(relative_path) {
        relative_path = relative_path.split('/').join('\\');
        var groups = this.xml.findall('ItemGroup');
        for (var i = 0, l = groups.length; i < l; i++) {
            var group = groups[i];
            var compiles = group.findall('Compile');
            for (var j = 0, k = compiles.length; j < k; j++) {
                var compile = compiles[j];
                if (compile.attrib.Include == relative_path) {
                    group.remove(0, compile);
                    return true;
                }
            }
        }
        return false;
    }
};

module.exports = csproj;

@@ -458,32 +458,72 @@
 /').join('%5C%5C');%0A
+        // make ItemGroup to hold file.%0A
         var item
@@ -546,32 +546,711 @@
 t('ItemGroup');%0A
+        // check if it's a .xaml page%0A        if(relative_path.indexOf('.xaml', relative_path.length - 5) %3E -1) %7B%0A            var page = new et.Element('Page');%0A            page.attrib.Include = relative_path;%0A            var gen = new et.Element('Generator');%0A            gen.text = %22MSBuild:Compile%22;%0A            page.append(gen);%0A            var sub_type = new et.Element('SubType');%0A            sub_type.text = %22Designer%22;%0A            page.append(sub_type);%0A            item.append(page);%0A        %7D%0A        // check if it's a .xaml.cs page that would depend on a .xaml of the same name%0A        else if (relative_path.indexOf('.xaml.cs', relative_path.length - 8) %3E -1) %7B%0A    
         var comp
@@ -1274,32 +1274,36 @@
 ent('Compile');%0A
+    
         compile.
@@ -1346,29 +1346,516 @@
     
-item.append(compile);
+    var dep = new et.Element('DependentUpon');%0A            var parts = relative_path.split('%5C%5C');%0A            var xaml_file = parts%5Bparts.length - 1%5D.substr(0, parts%5Bparts.length - 1%5D.length - 3);%0A            dep.text = xaml_file;%0A            compile.append(dep);%0A            item.append(compile);%0A        %7D%0A        // otherwise add it normally%0A        else %7B%0A            var compile = new et.Element('Compile');%0A            compile.attrib.Include = relative_path;%0A            item.append(compile);%0A        %7D
 %0A   
@@ -2015,24 +2015,29 @@
         var 
+item_
 groups = thi
@@ -2092,16 +2092,21 @@
  0, l = 
+item_
 groups.l
@@ -2150,16 +2150,21 @@
 group = 
+item_
 groups%5Bi
@@ -2182,20 +2182,17 @@
     var 
-comp
+f
 iles = g
@@ -2206,32 +2206,62 @@
 ndall('Compile')
+.concat(group.findall('Page'))
 ;%0A            fo
@@ -2278,20 +2278,17 @@
  0, k = 
-comp
+f
 iles.len
@@ -2330,22 +2330,16 @@
 var 
-comp
+f
 ile = 
-comp
+f
 iles
@@ -2363,20 +2363,17 @@
     if (
-comp
+f
 ile.attr
@@ -2427,33 +2427,346 @@
     
-group.remove(0, compile);
+// remove file reference%0A                    group.remove(0, file);%0A                    // remove ItemGroup if empty%0A                    var new_group = group.findall('Compile').concat(group.findall('Page'));%0A                    if(new_group.length %3C 1) %7B%0A                        this.xml.getroot().remove(0, group);%0A                    %7D
 %0A   

Support for adding UI elements to windows phone plugins