Generate XmlWriter c# code from XML

Go To StackoverFlow.com

0

I have an XML file that I need to create in code using XmlWriter. Is there any code generator that will take the XML and generate the lines of c# code to recreate it using XmlWriter?

2012-04-03 20:42
by user31673
Why use XmlWriter? It's much simpler to use LINQ to XML. And, no, I've never heard of such a tool - John Saunders 2012-04-03 22:36


1

I took Anurag's response, modified it, and here is what is working for me:

<#@ template language="C#" #> 
<#@ assembly name="System.Core" #> 
<#@ assembly name="System.Xml" #> 
<#@ import namespace="System.Xml" #> 
<#@ import namespace="System.Collections.Generic" #> 
<#@ import namespace="System.IO" #> 
using System; 
using System.Text; 
using System.Xml; 

 namespace AutoGenerateXmlWriteCode 
 { 
    public class TestClass 
   { 
    #region Methods 

        public static void WriteXml() 
        { 
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = "\t";
            settings.OmitXmlDeclaration = true;

            using( var writer = XmlWriter.Create("out.xml", settings)) 
        { 
<# 
  foreach (XmlReader node in this.GetNames())    
    { 
        if(node.NodeType == XmlNodeType.Element) { 
#> 
            writer.WriteStartElement(@"<#= node.Name  #>"); // <#= node.Name  #> 
<#
            if (node.HasAttributes)
            {
                node.MoveToFirstAttribute();
#> 
                    writer.WriteAttributeString(@"<#= node.Name  #>", @"<#= node.Value  #>");
 <#
                while (node.MoveToNextAttribute())
                {
 #> 
                    writer.WriteAttributeString(@"<#= node.Name  #>", @"<#= node.Value  #>");
 <#
               }
                node.MoveToElement();
            }

            if (node.IsEmptyElement){
#>

            writer.WriteEndElement(); // <#= node.Name  #> 
<#
            }
        } 
        if(node.NodeType == XmlNodeType.Text) { 
#> 
            writer.WriteValue(@"<#= node.Value  #>");
<#      } 
        if(node.NodeType == XmlNodeType.EndElement) { 
#> 
            writer.WriteEndElement();  // <#= node.Name  #>
<#      } 
} 
#> 
    } 
  } 

    #endregion 
 } 
} 

<#+ 

 public IEnumerable<XmlReader> GetNames() 
 { 
    List<string> result = new List<string>();  
    string absolutePath = @"d:\MyFile.xml";                 
    XmlReader rdr = XmlReader.Create(absolutePath);
    while (rdr.Read())
    {
        yield return rdr;
    }
} 

#>
2012-04-05 00:38
by user31673
I am glad it worked out for yo - Anurag Ranjhan 2012-04-05 02:24


2

You can write your own T4 Template

Here is some code to get you started:

<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<# assembly name="System.Xml" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
using System;
using System.Text;
using System.Xml;

 namespace Test
 {
    public class TestClass
   {
    #region Methods

        public static void WriteXml()
        {

    using( var writer = XmlWriter.Create("out.xml"))
    {
<#
  foreach (XmlNode node in this.GetNames())   
{
    if(node.NodeType == XmlNodeType.Element) {
#>
        writer.WriteStartElement(@"<#= node.Name  #>");
<# }
 if(node.NodeType == XmlNodeType.Comment) {
#>
    writer.WriteComment(@"<#= node.Value   #>");
<# }
}
#>
    }
  }

    #endregion
 }
}
<#+

 public IEnumerable<XmlNode> GetNames()
 {
    List<string> result = new List<string>(); 
    XmlDocument doc = new XmlDocument();        
    string absolutePath = @"c:\data\XMLFile1.xml";                
    doc.Load(absolutePath);
    foreach (XmlNode node in doc.ChildNodes)
    {
        yield return node;
    }

}
#>
2012-04-04 05:42
by Anurag Ranjhan
This is almost working great except that it only returns the first node of the xml file. Do you know how I can iterate through all the nodes in the xml file - user31673 2012-04-04 22:54
I figured out what I needed to change and posted the code below. Thank you so much for giving me this direction. It was VERY helpful! - user31673 2012-04-05 00:39


0

Visual studio's Xsd.exe may help you

2012-04-03 20:48
by hkutluay
That creates classes but not straight-forward XmlWriter cod - user31673 2012-04-03 21:00
Ads