如何将包含对象列表的对象转换为xml格式的层次结构 [英] How to convert a object which contains a list of objects as hierarchy in xml format

查看:100
本文介绍了如何将包含对象列表的对象转换为xml格式的层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i需要将对象转换为xml以下格式。我可以为输入节点创建xml。但不确定如何制作完整的包含所有级别节点的xml。





 < ;   ExecuteProcessTemplates  >  
< ExecuteProcessTemplate >
< ProjectID > ProjectIDValue < ; / ProjectID >
< ProcessTemplateID > ProcessTemplateIDValue < ; / ProcessTemplateID >
< 输入 >
< 输入 >
< Id > IDValue < / Id >
< 类型 > 资产< /类型 >
< > A ssetIDValue < / Value >
< /输入 > v

< /输入 >
< / ExecuteProcessTemplate >
< / ExecuteProcessTemplates >







为输入节点编写的代码



列表<输入>输入= 列表<输入>(); 

Inputs.Add( new 输入(){Id = 121 ,类型= 资产,值= 232});
ExecuteProcessTemplate ExecuteProcessTemplates = new ExecuteProcessTemplate();
ExecuteProcessTemplates.Inputs =输入;
var xEle = new XElement( 输入
来自 param in 输入
选择 new XElement( 输入
new XAttribute( Id,param.Id),
new XElement( Type,param.Type),
new XElement( Value,param.Value)
));





Inputs / ExecuteProcessTemplate的数据类:



  public   class  ExecuteProcessTemplate 
{
public long ProjectID { get ; set ; }
public long ProcessTemplateID { get ; set ; }
public 列表<输入>输入{ get ; set ; }

}
// 流程模板输入
public class 输入
{
public int ID { get ; set ; }
public string 输入{ get ; set ; }
public string 值{ get ; set ; }
}





任何人都可以帮我在c#中将对象的heirachy转换为xml。 />


先谢谢。

解决方案

至少有两种方式:

1)序列化和反序列化 [ ^ ]

自定义类集合序列化和反序列化的完整示例 [ ^ ]





2) LinqToXML [ ^ ]



示例(使用LinqPad [ ^ ]):

  void  Main()
{

List< ExecuteProcessTemplate> epts = new 列表< ExecuteProcessTemplate> {
new ExecuteProcessTemplate( 123456 789456
new 列表<输入> {< span class =code-keyword> new 输入( 1 A B), new 输入( 2 A C),
< span class =code-keyword> new 输入( 3 D),新的输入( 4 A E)}),
new ExecuteProcessTemplate( 123457 789457
new 列表<输入> {输入( 1 B C), new 输入( 2 B D),
new 输入( 3 B, E), new 输入( 4 B F)})
};

// epts.Dump();

XElement EptsToXmls =
new XElement( 来自 ept 的执行过程模板
epts 选择 new XElement( ExecuteProcessTemplate
new XElement( ProjectID,ept.ProjectID),
new XElement( ProcessTemplateID,ept.ProcessTemplateID),
new XElement( 输入
来自 inp ept.Inputs 选择
XElement( 输入
new XElement( Id,inp.Id),
new XElement( 键入,inp.Type),
new XElement( Value,inp.Value)
))));

EptsToXmls.Dump();
EptsToXmls.Save( @ D:\ _epts.xml);

}

// 在此处定义其他方法和类
public class ExecuteProcessTemplate
{
private long pid = 0 ;
private long ptid = 0 ;
private 列表<输入> 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 列表<输入>输入
{
get { return lst;}
set {lst = value ;}
}

}
// 流程模板输入
public class 输入
{
private int id = 0 ;
private string typ = string .Empty;
private string val = string .Empty;

public 输入( int _id, string _typ, string _val)
{
id = _id;
typ = _typ;
val = _val;
}

public int Id
{
get { return id;}
set {id = value ;}
}
public string 键入
{
get { return typ;}
set {typ = value ;}
}
public string 价值
{
获取 {返回 val;}
set {val = value ;}
}
}





结果:

 <   ExecuteProcessTemplates  >  
< ExecuteProcessTemplate >
< ProjectID > ; 123456 < / ProjectID >
< ProcessTemplateID > ; 789456 < / ProcessTemplateID >
< 输入 > ;
< 输入 >
< span class =code-keyword>< Id > 1 < / Id >
< span class =code-keyword>< 类型 > A < /类型 >
< span class =code-keyword>< > B < / Value >
< span class =code-keyword>< /输入 >
< 输入 >
< Id > 2 < / Id >
< 类型 > < / Type >
< > C < / Value >
< /输入 >
< 输入 >
< ID > 3 < / Id >
< 输入 > < / Type >
< > D < / Value >
< /输入 >
< 输入 >
< ID > 4 < < span class =code-leadattribute> / Id >
< 类型 > < < span class =code-leadattribute> / Type >
< > E < < span class =code-leadattribute> / Value >
< /输入 >
< /输入 >
< / ExecuteProcessTemplate >
< ExecuteProcessTemplate >
< ProjectID > 123457 < / ProjectID >
< ProcessTemplateID > 789457 < / ProcessTemplateID >
< 输入 >
< 输入 >
< Id > 1 < / Id >
< 类型 > B < /类型 >
< > C < / Value >
< /输入 >
< 输入 >
< ID > 2 < / Id >
< 类型 > B < / Type >
< > D < / Value >
< /输入 >
< 输入 < span class =code-keyword>>
< Id > 3 < / Id < span class =code-keyword>>
< 类型 > B < / Type < span class =code-keyword>>
< > E < /值 >
< /输入 >
< 输入 >
< ID > 4 < / Id >
< 类型 > B < / Type >
& lt; > F < / Value >
< /输入 >
< /输入 >
< / ExecuteProcessTemplate >
< / ExecuteProcessTemplates >


没有XML格式。可以有许多不同的方法将数据映射到XML,或使用某种XML格式持久化。我建议大多数实用的序列化方法,最强大和最容易使用,也非常非侵入性: https://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx [ ^ ]。



另见我过去的答案,我提倡这种方法:

如何在表单应用程序中使用XML文件编写器和阅读器? [ ^ ],

创建属性文件...... [ ^ ]。



-SA

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.

解决方案

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>


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


这篇关于如何将包含对象列表的对象转换为xml格式的层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆