XML响应带有额外的标签

时间:2020-08-10 16:07:37

标签: c# xml response webapi

我正在研究Web API服务并返回XML作为响应。

下面是我的模型课

public class School
{
  public List<StudentDetails> students {get;set;}
}
public class StudentDetails
{
  public string Name {get;set;}
  public int Age {get;set;}
}

我的控制器操作方法代码

School test = new School();
StudentDetails s1 = new StudentDetails();
s1.Name = "ABC"; s1.Age=25;

StudentDetails s2 = new StudentDetails();
s2.Name = "DEF"; s2.Age=35;

test.students.Add(s1);
test.students.Add(s2);

return Request.CreateResponse(HttpStatusCode.OK, test);

我的XML响应

<School xmln:i="http......> //not typing complete text here
 <students>
  <StudentDetails>
   <Age>25</Age>
   <Name>ABC</Name>
  </StudentDetails>
  <StudentDetails>
   <Age>35</Age>
   <Name>DEF</Name>
  </StudentDetails>
 </students>
</School>

在这里,我为什么得到<StudentDetails>标签?相反,我期望<students>就位。

1 个答案:

答案 0 :(得分:0)

您可以尝试添加类修饰,以更改序列化的XML元素名称。在下面查看。

c#,版本1

void Main()
{
    const string outputFile = @"e:\temp\SerializedFile.xml";
    
    
    StudentDetails s1 = new StudentDetails();
    s1.Name = "ABC"; s1.Age = 25;

    StudentDetails s2 = new StudentDetails();
    s2.Name = "DEF"; s2.Age = 35;

    School school = new School();
    school.students = new List<StudentDetails>();
    school.students.Add(s1);
    school.students.Add(s2);

    // save new XML file, serialized from classes
    using (StreamWriter sw = new StreamWriter(outputFile))
    {
        XmlSerializer x = new XmlSerializer(typeof(School));
        // to remove not needed namespaces
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("", "");
        // create new XML file
        x.Serialize(sw, school, ns);
    }
    
    Console.WriteLine("Classes were serialized as '{0}' file.", outputFile);
}


[XmlRoot("school")]
public class School
{
    [XmlElement(ElementName = "student")]
    public List<StudentDetails> students { get; set; }
}

public class StudentDetails
{
    public string Name { get; set; }
    public int Age { get; set; }
}

输出

<?xml version="1.0" encoding="utf-8"?>
<school>
  <student>
    <Name>ABC</Name>
    <Age>25</Age>
  </student>
  <student>
    <Name>DEF</Name>
    <Age>35</Age>
  </student>
</school>

c#,版本2

void Main()
{
    string outputXML = string.Empty;

    StudentDetails s1 = new StudentDetails();
    s1.Name = "ABC"; s1.Age = 25;

    StudentDetails s2 = new StudentDetails();
    s2.Name = "DEF"; s2.Age = 35;

    School school = new School();
    school.students = new List<StudentDetails>();
    school.students.Add(s1);
    school.students.Add(s2);

    // save new XML file, serialized from classes
    var settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.OmitXmlDeclaration = true;
    settings.ConformanceLevel = ConformanceLevel.Auto;
    settings.IndentChars = "\t";
    // to remove BOM
    settings.Encoding = new UTF8Encoding(false);

    using (var sw = new StringWriter())
    {
        using (XmlWriter xw = XmlWriter.Create(sw, settings))
        {
            XmlSerializer x = new XmlSerializer(typeof(School));
            // to remove not needed namespaces
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "");
            // create new XML file
            x.Serialize(xw, school, ns);
        }

        outputXML = sw.ToString();
    }

    Console.WriteLine("Classes were serialized as an XML string:{1}{0}", outputXML, Environment.NewLine);
}

// Define other methods and classes here
[XmlRoot("school")]
public class School
{
    [XmlElement(ElementName = "student")]
    public List<StudentDetails> students { get; set; }
}

public class StudentDetails
{
    public string Name { get; set; }
    public int Age { get; set; }
}