如何在没有命名空间的情况下将Object转换为XML?

时间:2015-08-12 11:14:09

标签: c# xml

我有这段代码,如何从输出xml中删除命名空间

MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(OrderTable));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, null);
xs.Serialize(xmlTextWriter, ot);
string result = Encoding.Default.GetString(memoryStream.ToArray());



<?xml version="1.0" ?> 
- <OrderTable xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ForceUpdate>false</ForceUpdate> 
  <TableId xsi:nil="true" /> 
  <UpdatedBy>Jack</UpdatedBy> 
  </OrderTable>

我想删除xmlns,我用谷歌搜索,但那些没有帮助我。

1 个答案:

答案 0 :(得分:1)

  

我想删除xmlns

您所要做的就是使用XmlSerializerNamespaces

var ns = new XmlSerializerNamespaces();
ns.Add("", "");
xs.Serialize(xmlTextWriter, ot, ns);