如何序列化为控制命名空间的XML

本文关键字:命名空间 XML 控制 序列化 | 更新日期: 2023-09-27 17:57:33

我需要生成以下XML:

<Learner xmlns="some.domain.api">
  <ActivationDate>1999-05-31T11:20:00</ActivationDate>
  <EmailAddress>String content</EmailAddress>
  <ExpirationDate>1999-05-31T11:20:00</ExpirationDate>
  <FederalId>String content</FederalId>
  <FirstName>String content</FirstName>
  <Grade>K</Grade>
  <LastName>String content</LastName>
  <MiddleName>String content</MiddleName>
  <UserName>String content</UserName>
  <Password>String content</Password>
  <SISId>String content</SISId>
  <StateId>String content</StateId>
  <Status>Active</Status>
</Learner>

我读过这些问题:

  • 如何使xmlserializer只序列化纯xml
  • 在Visual Studio IDE中自动生成XSD到C#

我正在使用Xsd2Code从XSD生成类那个正在起作用。

我试过这个:

private static string SerializeXML(Object obj)
{
    XmlSerializer serializer = new XmlSerializer(obj.GetType(), "some.domain.api");
    //settings
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.OmitXmlDeclaration = true;
    StringWriter stream = new StringWriter();
    XmlWriter writer = XmlWriter.Create(stream, settings);
    serializer.Serialize(writer, obj);
    return stream.ToString();
}

但它产生了这个:

<Learner xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="some.domain.api">
  <ActivationDate>1999-05-31T11:20:00</ActivationDate>
  <EmailAddress>String content</EmailAddress>
  <ExpirationDate>1999-05-31T11:20:00</ExpirationDate>
  <FederalId>String content</FederalId>
  <FirstName>String content</FirstName>
  <Grade>K</Grade>
  <LastName>String content</LastName>
  <MiddleName>String content</MiddleName>
  <UserName>String content</UserName>
  <Password>String content</Password>
  <SISId>String content</SISId>
  <StateId>String content</StateId>
  <Status>Active</Status>
</Learner>

我想摆脱xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"

如何?

更新

我也已经用XmlSerializerNamespaces尝试过了。如果我添加这样的名称空间,使用上面的实现并进行必要的调整:

XmlSerializerNamespaces namespaces = 
    new XmlSerializerNamespaces(new[] { new XmlQualifiedName("ns","some.domain.api") });

我得到这个:

<Learner xmlns:ns="some.domain.api">

但我需要这个:

<Learner xmlns="some.domain.api">

如果我添加这样的名称空间,使用上面的实现并进行必要的调整:

XmlSerializerNamespaces namespaces = 
    new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });

我得到这个:

<q1:Learner xmlns:ns="some.domain.api">

用于所有XML标记!

我有什么办法可以得到我想要的输出吗?我真的不想用StringBuilder来完成这个任务。。。

如何序列化为控制命名空间的XML

您可以使用

XmlSerializerNamespaces namespaces = 
new XmlSerializerNamespaces(new[] { new XmlQualifiedName("ns","some.domain.api") });

并在写入文件后执行以下操作:

StreamReader reader = new StreamReader(file_name);
string content = reader.ReadToEnd();
reader.Close();
content = Regex.Replace(content, ":ns", "");
StreamWriter writer = new StreamWriter(file_name);
writer.Write(content);
writer.Close();

不是最优雅的,但它很管用。

诀窍是为命名空间使用一个空名称:

XmlSerializerNamespaces namespaces = 
new XmlSerializerNamespaces(new[] { new XmlQualifiedName("","some.domain.api") });

然后将它们添加到序列化过程中:

serializer.Serialize(writer, obj, namespaces);

这就是省略"ns:"的全部内容。

试试这样的东西:

 public static string SerializeObject(object obj)
    {
        XmlSerializerNamespaces XSN = new XmlSerializerNamespaces();
        XSN.Add("bs", "some.domain.api");
        XmlWriterSettings XWS = new XmlWriterSettings();
        XWS.OmitXmlDeclaration = true;
        Stream stream = new MemoryStream();
        XmlTextWriter xtWriter = new XmlTextWriter(stream, new UTF8Encoding(false));
        XmlSerializer ser = new XmlSerializer(obj.GetType());
        ser.Serialize(xtWriter, obj, XSN);
        xtWriter.Flush();
        stream.Seek(0, System.IO.SeekOrigin.Begin);
        string xml = Encoding.UTF8.GetString(((MemoryStream)stream).ToArray());
        return xml;
    }

更新

如果您被迫使用固定的xml输出语法,您总是可以将返回的xml字符串"xmlns:bs"替换为"xmlns"!