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

发布于 2024-11-09 10:39:33 字数 3522 浏览 0 评论 0原文

我需要生成以下 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 来完成这项任务......

I need to generate the following 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>

I have read these questions:

I am using Xsd2Code to generate the classes from XSD. That is working.

I have tried this:

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

But it produces this:

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

I want to get rid of xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema".

How to?

Update

I have also already tried this using XmlSerializerNamespaces. If I add namespaces like this, using my implementation above with the necessary adjustments:

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

I get this:

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

But I need this:

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

If I add namespaces like this, using my implementation above with the necessary adjustments:

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

I get this:

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

for all XML tags!

Is there any way I can do to get my desired output? I really don't want to use StringBuilder for this task...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执笏见 2024-11-16 10:39:33

您可以像上面那样做:

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

不是最优雅的,但它可以工作。

You can do as you did up there with:

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

and after writing the file do this:

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

Not the most elegant, but it works.

Bonjour°[大白 2024-11-16 10:39:33

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

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

然后为序列化过程添加它们:

serializer.Serialize(writer, obj, namespaces);

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

The trick is to use an empty name for the namespace:

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

Then you add them for the serialization process:

serializer.Serialize(writer, obj, namespaces);

Thats all to omit the "ns:".

最后的乘客 2024-11-16 10:39:33

尝试这样的事情:

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

try something like this:

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

updated

You can always replace the returning xml string "xmlns:bs" to "xmlns" if you are forced to a fixed xml output syntax!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文