首页 > 解决方案 > 带有额外标签的 XML 响应

问题描述

我正在开发 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>到位。

标签: c#xmlresponsewebapi

解决方案


您可以尝试添加将更改序列化 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; }
}

推荐阅读