Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.


XML
<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

C#
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:

C#
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?

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
 
Share this answer
 
Comments
[no name] 16-Mar-15 23:59pm    
Thanq for your responese Sergey.
Sergey Alexandrovich Kryukov 17-Mar-15 0:10am    
You are welcome, but this is an actual solution. Will you accept it formally?
—SA
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[^]):
C#
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:
XML
<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>
 
Share this answer
 
v2
Comments
[no name] 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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900