2013-04-11 157 views
-2

我有3个实体作为目标。任务和提醒。对于每个实体我有单独的数据表。现在我想生成如下的xml用c动态生成xml#

<PDPData> 
    <Goal> 
    <TypeId>300</TypeId> 
    <NAME>Smart Goal #</NAME> 
    <Task> 
     <TypeId>11</TypeId> 
     <NAME>Task1</NAME> 
    </Task> 
    <Task> 
     <TypeId>12</TypeId> 
     <NAME>Task2</NAME> 
    </Task> 
    <Reminder> 
     <TypeId>11</TypeId> 
     <NAME>Reminder1</NAME> 
    </Reminder> 
    <Reminder> 
     <TypeId>12</TypeId> 
     <NAME>Reminder2</NAME> 
    </Reminder> 
    </Goal> 
</PDPData> 

我该如何实现这一点。我试着用下面的代码,但其附加只有一个孩子,但我想追加和目标内

XmlElement hedder = docConfig.CreateElement("Goal"); 
docConfig.DocumentElement.PrependChild(hedder); 
docConfig.ChildNodes.Item(0).AppendChild(hedder); 

// Create <installationid> Node 
XmlElement installationElement = docConfig.CreateElement("TypeId"); 
XmlText installationIdText = docConfig.CreateTextNode(dtGoals.Rows[goalCount]["TypeId"].ToString()); 
installationElement.AppendChild(installationIdText); 
hedder.AppendChild(installationElement); 

// Create <environment> Node 
XmlElement environmentElement = docConfig.CreateElement("NAME"); 
XmlText environText = docConfig.CreateTextNode(dtGoals.Rows[goalCount]["Name"].ToString()); 
environmentElement.AppendChild(environText); 
hedder.AppendChild(environmentElement); 
+2

您的第一个操作应该是搜索谷歌。这已被多次记录! – 2013-04-11 06:51:00

+0

我试过并在那里搜索,但我无法找到正确的解决方案。 – nrsharma 2013-04-11 06:52:06

+0

显示给定XML的类定义和样例数据。 – MarcinJuraszek 2013-04-11 06:53:38

回答

-2
using System; 
public class clsPerson { 
    public string FirstName; 
    public string MI; 
    public string LastName; 
} 

class class1 { 
static void Main(string[] args) 
{ clsPerson p=new clsPerson(); p.FirstName = "Jeff"; p.MI = "A"; p.LastName = "Price"; System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType()); x.Serialize(Console.Out, p); Console.WriteLine(); 
Console.ReadLine(); } } 

抱歉格式,可以从手机发布。

+0

这不是一个非常有用的答案。也许你可以提供一个例子? – 2013-04-11 06:55:44

0
[Serializable()] //Set this attribute to all the classes that want to serialize 
public class Employee : ISerializable 
{ 
    public int EmpId; 
    public string EmpName; 

    //Default constructor 
    public Employee() 
    { 
     EmpId = 0; 
     EmpName = null; 
    } 
} 

XmlSerializer serializer = new XmlSerializer(typeof(Employee)); 
TextWriter writer = new StreamWriter(filename); 
Employee objEmp = new Employee(); 
serializer.Serialize(writer, objEmp); 
writer.Close();