[編集]
:それへのXMLスキーマは、それはこのようになります
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(...);
この情報がお役に立てば幸いです...詳細は
感謝を使用しないでください。素晴らしい答え! –
ここで、 'XmlSchema.Namespace'と' XmlSchema.InstanceNamespace'の定数を利用できることに注意してください。 – tm1