摆脱 xsi:type 并使用标签名称来代替

问题描述 投票:0回答:1

我有一些派生类要序列化为 XML:

public abstract class Level
{
    
}

[XmlRoot("SubLevel1")]
public class SubLevel1 : Level
{
    public string prop1 { get; set; }

    public SubLevel1()
    {

    }

    public SubLevel1(string prop)
    {
        this.prop1 = prop;
    }
}

[XmlRoot("SubLevel2")]
public class SubLevel2 : Level
{
    [XmlText]
    public string prop2 { get; set; }

    public SubLevel2()
    {

    }

    public SubLevel2(string s)
    {
        this.prop2 = s;
    }
}

以及收藏类:

[XmlInclude(typeof(SubLevel1))]
[XmlInclude(typeof(SubLevel2))]
[XmlRoot("LevelCollection")]
public class LevelCollection : ICollection<Level>
{
    protected List<Level> m_Levels;

    public List<Level> Levels
    {
        get { return m_Levels; }
        set { m_Levels = value; }
    }

    public LevelCollection()
    {
        m_Levels = new List<Level>();
    }
}

要序列化的对象:

Levels.LevelCollection collection = new Levels.LevelCollection();
collection.Add(new Levels.SubLevel1("aaa"));
collection.Add(new Levels.SubLevel2("bbb"));

XmlSerializer serializer = new XmlSerializer(typeof(Levels.LevelCollection));
StringWriter sw = new StringWriter();
XmlWriter writer = XmlWriter.Create(sw);
serializer.Serialize(writer, obj);
string xml = sw.ToString();

序列化后的 XML 如下所示(包含 xsi:type 属性):

<?xml version="1.0" encoding="utf-8"?>
<LevelCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Level xsi:type="SubLevel1">
    <prop1>aaa</prop1>
  </Level>
  <Level xsi:type="SubLevel2">bbb</Level>
</LevelCollection>

是否可能以及如何实现将目标 XML 更改为:

<?xml version="1.0" encoding="utf-8"?>
<LevelCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SubLevel1>
    <prop1>aaa</prop1>
  </SubLevel1>
  <SubLevel2>bbb</SubLevel2>
</LevelCollection>

除了编写自定义编写器之外,我找不到答案。我想知道是否可以使用标准属性删除类型属性。

c# .net xml xml-serialization
1个回答
0
投票

未提供最小的可重现示例。在这种情况下,请尝试以下概念示例。它显示了如何防止输出 XML 中的 XML 命名空间。

它通过 C# 代码中的注释进行了大量记录。在您的代码中,您需要替换正确的类型,而不是序列化部分中的

school
类型。

c#

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; }
}

输出XML ABC 25 DEF 35

© www.soinside.com 2019 - 2024. All rights reserved.