Click here to Skip to main content
14,218,548 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi ,

i need to convert an object to below xml format.i can able to create xml for Inputs node.but not sure on how to make a complete xml with all levels of nodes.


<ExecuteProcessTemplates>
	<ExecuteProcessTemplate>
		<ProjectID>ProjectIDValue</ProjectID>
		<ProcessTemplateID>ProcessTemplateIDValue</ProcessTemplateID>
		<Inputs>
			<Input>
				<Id>IDValue</Id>
				<Type>Asset</Type>				
				<Value>AssetIDValue</Value>
			</Input>v

		</Inputs>		
	</ExecuteProcessTemplate>
</ExecuteProcessTemplates>



code written for inputs node

List<Input> Inputs = new List<Input>();

           Inputs.Add(new Input() {Id=121,Type="Asset",Value="232" });
           ExecuteProcessTemplate ExecuteProcessTemplates = new ExecuteProcessTemplate();
           ExecuteProcessTemplates.Inputs = Inputs;
           var xEle = new XElement("Inputs",
               from param in Inputs
               select new XElement("Input",
                            new XAttribute("Id", param.Id),
                              new XElement("Type", param.Type),
                              new XElement("Value", param.Value)
                              ));


Data class for Inputs/ExecuteProcessTemplate:

public class ExecuteProcessTemplate
{
    public long ProjectID { get; set; }
    public long ProcessTemplateID { get; set; }
    public List<Input> Inputs { get; set; }

}
//Process template inputs
public class Input
{
    public int Id { get; set; }
    public string Type { get; set; }
    public string Value { get; set; }
}


Can any body help me to convert heirachy of object to xml in c#.

Thanks in Advance.
Posted
Comments
Andy Lanng 16-Mar-15 11:44am
   
Have you looked at serialization?
http://stackoverflow.com/questions/890763/how-to-serialize-a-list-of-lists-with-the-type-of-custom-object
Maybe a quick win for you
Maciej Los 16-Mar-15 11:44am
   
What have you tried?
Rate this:
Please Sign up or sign in to vote.

Solution 1

There are 2 ways at least:
1) serialization and deserialization[^]
A Complete Sample of Custom Class Collection Serialization and Deserialization[^]

and
2) LinqToXML[^]

Example (using LinqPad[^]):
void Main()
{
	
	List<ExecuteProcessTemplate> epts = new List<ExecuteProcessTemplate>{
		new ExecuteProcessTemplate(123456, 789456,
			new List<Input>{new Input(1, "A", "B"),new Input(2, "A", "C"),
				new Input(3, "A", "D"),new Input(4, "A", "E")}),
		new ExecuteProcessTemplate(123457, 789457,
			new List<Input>{new Input(1, "B", "C"),new Input(2, "B", "D"),
				new Input(3, "B", "E"),new Input(4, "B", "F")})
	};
	
	//epts.Dump();

	XElement EptsToXmls = 
    new XElement("ExecuteProcessTemplates",
		from ept in epts select new XElement("ExecuteProcessTemplate",
            	new XElement("ProjectID", ept.ProjectID),
            	new XElement("ProcessTemplateID", ept.ProcessTemplateID), 
				new XElement("Inputs", 
			from inp in ept.Inputs select 
            		new XElement("Input", 
                new XElement("Id", inp.Id),
            	new XElement("Type", inp.Type),
                new XElement("Value", inp.Value)
				))));
    
	EptsToXmls.Dump();
	EptsToXmls.Save(@"D:\epts.xml");

}

// Define other methods and classes here
    public class ExecuteProcessTemplate
    {
		private long pid =0;
		private long ptid =0;
		private List<Input> lst = new List<Input>();
		
		public ExecuteProcessTemplate(long _pid, long _ptid, List<Input> _lst)
		{
			pid = _pid;
			ptid = _ptid;
			lst = _lst;
		}
				
        public long ProjectID
		{
			get{return pid;}
			set{pid = value;}
		}
        
		public long ProcessTemplateID
		{ 
			get{return ptid;}
			set{ptid = value;}
		}
		
        public List<Input> Inputs 
		{
			get{return lst;}
			set{lst = value;}
		}
    
    }
    //Process template inputs
    public class Input
    {
		private int id = 0;
		private string typ = string.Empty;
		private string val = string.Empty;
		
		public Input(int _id, string _typ, string _val)
		{
			id = _id;
			typ = _typ;
			val = _val;
		}
		
        public int Id
		{
			get{return id;}
			set{id = value;}
		}
        public string Type
		{
			get{return typ;}
			set{typ = value;}
		}
        public string Value
		{
			get{return val;}
			set{val = value;}
		}
    }


Result:
<ExecuteProcessTemplates>
  <ExecuteProcessTemplate>
    <ProjectID>123456</ProjectID>
    <ProcessTemplateID>789456</ProcessTemplateID>
    <Inputs>
      <Input>
        <Id>1</Id>
        <Type>A</Type>
        <Value>B</Value>
      </Input>
      <Input>
        <Id>2</Id>
        <Type>A</Type>
        <Value>C</Value>
      </Input>
      <Input>
        <Id>3</Id>
        <Type>A</Type>
        <Value>D</Value>
      </Input>
      <Input>
        <Id>4</Id>
        <Type>A</Type>
        <Value>E</Value>
      </Input>
    </Inputs>
  </ExecuteProcessTemplate>
  <ExecuteProcessTemplate>
    <ProjectID>123457</ProjectID>
    <ProcessTemplateID>789457</ProcessTemplateID>
    <Inputs>
      <Input>
        <Id>1</Id>
        <Type>B</Type>
        <Value>C</Value>
      </Input>
      <Input>
        <Id>2</Id>
        <Type>B</Type>
        <Value>D</Value>
      </Input>
      <Input>
        <Id>3</Id>
        <Type>B</Type>
        <Value>E</Value>
      </Input>
      <Input>
        <Id>4</Id>
        <Type>B</Type>
        <Value>F</Value>
      </Input>
    </Inputs>
  </ExecuteProcessTemplate>
</ExecuteProcessTemplates>
   
v2
Comments
syedkhaleel 16-Mar-15 23:35pm
   
Thanks,i got really a good result.Thanks for your response..
Maciej Los 17-Mar-15 2:43am
   
You're very welcome. By The Way: you can accept all valuables answers. If it was helpful, please do not forget to use voting-system.
Rate this:
Please Sign up or sign in to vote.

Solution 2

There is no just "XML format". There could be many different ways to map data to XML, or to persist using some XML format. I would suggest on most practical way of doing serialization, most robust and easiest to use, also very non-intrusive: https://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^].

See also my past answers where I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

—SA
   
Comments
syedkhaleel 16-Mar-15 23:59pm
   
Thanq for your responese Sergey.
   
You are welcome, but this is an actual solution. Will you accept it formally?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100