我正在尝试将对象序列化为XML。我有一个问题,默认情况下会添加xsi和xsd。我添加了以下代码来删除命名空间:
public string CommitDocumentToRepository(string extension, byte[] fileBytes)
{
//Convert to PDF
byte[] loPDFFileBytes;
ConversionService.ContentResponse loResponse = new DocumentAdapter.ConversionService.ContentResponse();
using (ConversionService.CustomPDFSoapClient loConversionServiceClient = new DocumentAdapter.ConversionService.CustomPDFSoapClient())
{
loResponse = loConversionServiceClient.OfficeToPDFContent(fileBytes, extension);
loPDFFileBytes = loResponse.ContentPDF;
}
if (loPDFFileBytes != null)
{
xform loDocContainer = new xform();
xformInstance loDocProperties = new xformInstance();
loDocProperties.FIRST_NAME= this.FirstName;
loDocProperties.LAST_NAME= this.LastName;
loDocProperties.MID_NAME = this.MidName;
loDocContainer.instance = loDocProperties;
string lsTempFile = System.IO.Path.GetTempFileName();
string lsXMLofProperties = loDocContainer.Serialize();
XmlDocument loDoc = new XmlDocument();
loDoc.LoadXml(lsXMLofProperties);
loDoc.Save(lsTempFile);
byte[] loFilePropertiesXML = Common.StreamFile(lsTempFile);
string lsReturnValue = string.Empty;
try
{
using (ISCommittalService.CommittalSoapClient loCommittalServiceClient = new DocumentAdapter.ISCommittalService.CommittalSoapClient())
{
lsReturnValue = loCommittalServiceClient.CommitDocumentByte(loPDFFileBytes, ".PDF", loFilePropertiesXML);
}
}
catch (Exception loException)
{
ADConnectionException loConnectionException = new ADConnectionException(loException);
throw loException;
}
return lsReturnValue;
}
else
return string.Empty;
}
public string Serialize()
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Encoding = Encoding.UTF8;
StringBuilder loBuilder = new StringBuilder();
XmlSerializer formatter = new XmlSerializer(this.GetType());
using (StringWriter loWriter = new StringWriter(loBuilder))
{
formatter.Serialize(loWriter, this,ns);
}
return loBuilder.ToString();
}
这是输出:
<?xml version="1.0" encoding="UTF-16"?>
<xform>
<instance>
<FIRST_NAME xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance"
d3p1:type="q4:string"
xmlns:q4="http://www.w3.org/2001/XMLSchema">John</FIRST_NAME>
<MID_NAME xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance"
d3p1:type="q3:string" xmlns:q3="http://www.w3.org/2001/XMLSchema"/>
<LAST_NAME xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance"
d3p1:type="q5:string"
xmlns:q5="http://www.w3.org/2001/XMLSchema">Doe</LAST_NAME>
</instance>
</xform>
如何从每个对象中删除添加的xmlns(xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance"
d3p1:type="q4:string"
xmlns:q4="http://www.w3.org/2001/XMLSchema"
)?
如何将其更改为utf-8,因为我的代码xws.Encoding = Encoding.UTF8;
似乎没有任何影响。