2011-05-23 52 views
4

我需要生成以下XML:如何序列化为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这个任务......

+0

供应商要求。去搞清楚。 – 2011-05-23 20:58:20

+0

@marc_s有什么想法?它看起来很琐碎,但我现在已经跑了几个小时了...... – 2011-05-23 21:10:48

+0

正确的方法是告诉对方清理自己的行为,并能够解析序列化的输出 - 绝对是这样积极合法的XML!不知道您是否可以将.NET XML序列化转换为让您希望破解的系统需求.... – 2011-05-23 21:11:47

回答

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

不是最优雅,但它的工作原理。

+0

这就是我所说的http://en.wikipedia.org/wiki/Egg_of_Columbus – 2011-05-23 22:04:50

+0

不要误解我的意思,它不够优雅,但我认为*我喜欢它:) – 2011-05-23 22:09:50

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字符串“的xmlns:BS “到”xmlns“,如果你被迫使用固定的xml输出语法!

+0

你的解决方案忽略了设置。输出根目录是<'。我需要让它='' – 2011-05-23 20:42:27

+0

因为它忽略了设置,所以不会省略XML声明。更重要的是,您的解决方案不提供缩进,'XWS.Indent = true;'丢失。 – 2011-05-23 20:57:38

2

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

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

然后添加他们的序列化进程:

serializer.Serialize(writer, obj, namespaces); 

这就是所有省略“NS”。

相关问题