将类序列化为XML C#

时间:2017-12-05 13:11:54

标签: c# .net xml class xml-serialization

我有以下类,即ReconciliationRequest.cs

using System;
using System.Collections.Generic;
using System.Xml.Serialization;

[XmlRoot("ReconciliationRequest")]
[Serializable]
public class ReconciliationRequest
{

    public List<Patient> PatientDetails;
}
[Serializable]

public class Patient
{
    [XmlElement("NHSNumber")]
    public string NHSNumber { get; set; }

    [XmlElement("PDSChangeSerialNumber")]
    public string PDSChangeSerialNumber { get; set; }

}

我想将其序列化为XML格式

<ReconciliationRequest xmlns="http://schemas.capita.co.uk/pcse/crm">
        <Patient>
            <NHSNumber>1234567890</NHSNumber>
            <PDSChangeSerialNumber>1234</PDSChangeSerialNumber>
        </Patient>




</ReconciliationRequest>

但是我将它变成以下格式,我不希望在xml中使用PatientDetails标签, 在上面提到的结果中我是这样的。

 <ReconciliationRequest>
      <PatientDetails>
        <Patient>
          <NHSNumber>9411992455</NHSNumber>
          <PDSChangeSerialNumber>5</PDSChangeSerialNumber>
        </Patient>
        <Patient>
          <NHSNumber>9801153512</NHSNumber>
          <PDSChangeSerialNumber>20</PDSChangeSerialNumber>
        </Patient>

      </PatientDetails>
    </ReconciliationRequest>

我不想 PatientDetails 节点。 下面是我用来将这个类对象序列化为XML

的代码
ReconciliationRequest obj = new ReconciliationRequest();
                Patient obj1;
                List<Patient> PatientDetails = new List<Patient>();
                if (results.Entities.Count > 0)
                {
                    var PatientData = results.Entities[0].ToEntity<Contact>();

                    foreach (var item in results.Entities)
                    {
                        obj1 = new Patient();
                        if (item.Attributes.Keys.Contains("scr_nhs_number") && item.Attributes.Keys.Contains("csr_pdsserialchangenumber"))
                        {
                            if (!string.IsNullOrEmpty(item.Attributes["scr_nhs_number"].ToString()))
                            {
                                obj1.NHSNumber = item.Attributes["scr_nhs_number"].ToString();
                            }
                            if (!string.IsNullOrEmpty(item.Attributes["csr_pdsserialchangenumber"].ToString()))
                            {
                                obj1.PDSChangeSerialNumber = item.Attributes["csr_pdsserialchangenumber"].ToString();
                            }

                            PatientDetails.Add(obj1);
                        }
                    }
                    obj.PatientDetails = PatientDetails;


                    XmlSerializer serializer =
       new XmlSerializer(typeof(ReconciliationRequest));
                    TextWriter writer = new StreamWriter("D:\\CS\\data3.xml");
                    XmlSerializerNamespaces nameSpace = new XmlSerializerNamespaces();
                    nameSpace.Add(string.Empty, "http://schemas.capita.co.uk/pcse/crm");

                    serializer.Serialize(writer, obj, nameSpace);
                    writer.Close();
                }

            }

请指导我如何解决此问题。

2 个答案:

答案 0 :(得分:3)

[XmlRoot("ReconciliationRequest", Namespace = "http://schemas.capita.co.uk/pcse/crm")]
public class ReconciliationRequest
{
    [XmlElement("Patient")]    
    public List<Patient> PatientDetails;
}

虽然我亲自使用:

[XmlElement("Patient")]
public List<Patient> PatientDetails { get; } = new List<Patient>();

答案 1 :(得分:1)

试试这个让你的类继承List

  angular.forEach($scope.data.availableOptions, function (option, index) {
                        if (option.id === 1) { // 1 ==> id of the option
                            $scope.data.model= option;
                        }
                    });

并像这样使用它。

    public class ReconciliationRequest<T> : List<T>
    {

    }

这将消除您的详细信息标记。