[Изменить]
Вы можете явно реализовать IXmlSerializable и написать / прочитать xml самостоятельно.
public class MyType : IXmlSerializable
{
void IXmlSerializable.WriteXml(XmlWriter writer)
{
writer.WriteAttributeString("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
writer.WriteAttributeString("xsi", "noNamespaceSchemaLocation", XmlSchema.InstanceNamespace, "mySchema.xsd");
// other elements & attributes
}
XmlSchema IXmlSerializable.GetSchema()
{
throw new NotImplementedException();
}
void IXmlSerializable.ReadXml(XmlReader reader)
{
throw new NotImplementedException();
}
}
xmlSerializer.Serialize(xmlWriter, myTypeInstance);
Скорее всего, не идеальное решение, но добавление следующего поля и атрибута в ваш класс поможет.
public class MyType
{
[XmlAttribute(AttributeName="noNamespaceSchemaLocation", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
public string Schema = @"mySchema.xsd";
}
Другой вариант - создать свой собственный класс XmlTextWriter.
xmlSerializer.Serialize(new CustomXmlTextWriter(xmlWriter), myTypeInstance);
Или не используйте сериализацию
var xmlDoc = new XmlDocument();
xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null));
var xmlNode = xmlDoc.CreateElement("MyType");
xmlDoc.AppendChild(xmlNode);
xmlNode.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
xmlNode.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
var schema = xmlDoc.CreateAttribute("xsi", "noNamespaceSchemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
schema.Value = "mySchema.xsd";
xmlNode.SetAttributeNode(schema);
xmlDoc.Save(...);
Надеюсь, это поможет ...