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

时间:2011-05-23 20:32:08

标签: c# xml serialization

我需要生成以下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>

我已经阅读了这些问题:

我正在使用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来完成这项任务......

3 个答案:

答案 0 :(得分:2)

你可以像在那里那样做:

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();

不是最优雅,但它有效。

答案 1 :(得分:2)

诀窍是为名称空间使用空名称:

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

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

serializer.Serialize(writer, obj, namespaces);

这就是省略“ns:”。

答案 2 :(得分:0)

尝试这样的事情:

 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”!