4

I have a xml template like this

<User>
<UserId></UserId>
<UserName></UserName>
<Roles>
<Role></Role>
<Roles></Role>
</Roles>
</User>

Form this xml template file,dynamicaly i want to generate an xml.How can i do this.

Output xml should look like this

<User>
<UserId>user1</UserId>
<UserName>fr</UserName>
<Roles>
<Role>abc</Role>
<Role>def</Role>
</Roles>
</User>

How can i achieve this.What all changes i need to make in template file.How to read and create xml from this tempalte xml file using c#.

user922834
  • 195
  • 2
  • 6
  • 18

4 Answers4

6

You can use XmlSerializer and create a simple class with properties according to the template:

public class User
{
   public UserId{get;set;}

   ...
}

And serialize the class to XML file.

There's a good example here.

Option #2:
If for some reason you do not want to use XmlSerializer use XmlWriter - in order to prevent forgetting to close elements I suggest you use "AutoClose" XmlWriter - I've blogged about how to create this simple class on my blog - XmlWriter that automatically close elements using IDisposable

dbc
  • 104,963
  • 20
  • 228
  • 340
Dror Helper
  • 30,292
  • 15
  • 80
  • 129
  • i am not plannig to use xmlserializer – user922834 Apr 04 '12 at 07:19
  • 1
    Why not? this way you only need to maintain code and do not need to write every single XML node "by hand" – Dror Helper Apr 04 '12 at 07:21
  • 1
    Better answer than what i gave – Nikhil Agrawal Apr 04 '12 at 07:23
  • i have list in which it contain three objects.I want to create an xml with all three object details,then how it will be? – user922834 Apr 04 '12 at 07:30
  • There are several attributes you can use to change the way that take a look at this SO question http://stackoverflow.com/questions/2292480/xmlserializer-list-item-element-name – Dror Helper Apr 04 '12 at 07:34
  • 1
    [XmlWriter that automatically close elements using IDisposable](http://blog.drorhelper.com/2009/07/xmlwriter-that-automatically-close.html) has a pretty good implementation with c# using and .Net IDisposable. If you'd like XmlWriter, this is a good way to avoid all the WriteEnd... methods. – gmaran23 May 28 '14 at 07:06
  • I do:) And I liked the way you of using the using. Seems pretty native c#. – gmaran23 Jun 02 '14 at 09:32
6

Serializing is an option, but if you really need a template, you can use this https://github.com/beto-rodriguez/Templator

here is an example

C#

UserClass user = new UserClass();
user.UserId = 1;
user.UserName = "my name";
User.Roles = new List<string>(){"admin", "sales"};
//some other properties...    

var compiler = new Compiler()
            .AddElementToScope("user", user);

var compiled = compiler.CompileXml(@"C:\...\myXml.xml")

XLM Source

<User>
  <UserId>{{user.UserId}}</UserId>
  <UserName>{{user.UserName}}</UserName>
  <Roles Tor.Repeat="role in user.Roles">
    <Role>{{role}}</Role>
  <Roles></Role>
  </Roles>
</User>

Compiled

<User>
  <UserId>1</UserId>
  <UserName>my name</UserName>
  <Roles>
    <Role>admin</Role>
    <Role>sales</Role>
  <Roles></Role>
  </Roles>
</User>

you can install it from Nuget too:

Install-Package SuperXML
Fenixp
  • 645
  • 5
  • 22
bto.rdz
  • 6,636
  • 4
  • 35
  • 52
  • 1
    can downvoter plase let me know why this is not an option? – bto.rdz Aug 07 '15 at 17:47
  • Exactly what I was looking for, so much more elegant with more complex templates than what you get with `XmlSerializer` and having to navigate a large document dotted with variables. – Dav Dec 28 '19 at 12:25
0
var templateXML = "C://template.xml"//full path for the template xml
XmlDocument doc = new XmlDocument();
FileStream fs = new FileStream(templateXML, FileMode.Open, FileAccess.Read);
doc.Load(fs);
XmlNode node = doc.DocumentElement;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("pf", "http://www.example.com/abc");
node = doc.DocumentElement;
nodeName = "/pf:Message/pf:User/pf:UserID";
node.SelectSingleNode(nodeName, nsmgr).InnerXml = "user1";put you custom value
nodeName = "/pf:Message/pf:User/pf:UserName";                
node.SelectSingleNode(nodeName, nsmgr).InnerXml = "fr";put you custom value
....
.....
.......
var postData = node.ParentNode.InnerXml;
postData = node.OuterXml;
TotPeRo
  • 6,561
  • 4
  • 47
  • 60
Rahul Sen
  • 81
  • 1
  • 12