C# 无法使用c格式化xml#

C# 无法使用c格式化xml#,c#,xml,class,oop,C#,Xml,Class,Oop,好的,我有两门课 public class PRData { public DateTime PRDate { get; set; } public string Title { get; set; } public string Description { get; set; } } 一个是 public class MonthData { public string Months { get; set; } public List<PRData

好的,我有两门课

public class PRData
{
    public DateTime PRDate { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
}
一个是

public class MonthData
{
    public string Months { get; set; }
    public List<PRData> PrList { get; set; }
}
现在我尝试将这个对象转换为xml

string xml = Helper.GetXMLFromObject(mon);
我收到的xml是

 <MonthData>
  <Months>feb</Months> 
  <PrList>
    <PRData>
      <PRDate>2012-02-01T00:00:00</PRDate> 
      <Title>hello</Title> 
      <Description>Hello world</Description> 
    </PRData>
    <PRData>
      <PRDate>2012-02-01T00:00:00</PRDate> 
      <Title>hello</Title> 
      <Description>Hello world</Description> 
     </PRData>
  </PrList>
</MonthData>
下面是我用来将对象转换为xml的函数

 public static string GetXMLFromObject(object o)
    {
        try
        {
            XmlSerializer XmlS = new XmlSerializer(o.GetType());

            StringWriter sw = new StringWriter();
            XmlTextWriter tw = new XmlTextWriter(sw);

            XmlS.Serialize(tw, o);

            return sw.ToString();
        }
        catch (Exception ex)
        {
            throw new DataAccessException("Could Not Serialize object : GetXMLFromObject" + " : " + ex.Message);

        }
    }
注意::
我期待的解决方案是在我的类中进行一些更改,而不是在我上面给出的将对象转换为xml的函数中进行更改

只需将
xmlement
属性应用于
PrList
属性:

using System.Xml.Serialization;

...

[XmlElement]
public List<PRData> PrList { get; set; }
使用System.Xml.Serialization;
...
[XmlElement]
公共列表PrList{get;set;}
 public static string GetXMLFromObject(object o)
    {
        try
        {
            XmlSerializer XmlS = new XmlSerializer(o.GetType());

            StringWriter sw = new StringWriter();
            XmlTextWriter tw = new XmlTextWriter(sw);

            XmlS.Serialize(tw, o);

            return sw.ToString();
        }
        catch (Exception ex)
        {
            throw new DataAccessException("Could Not Serialize object : GetXMLFromObject" + " : " + ex.Message);

        }
    }
using System.Xml.Serialization;

...

[XmlElement]
public List<PRData> PrList { get; set; }