Я использую XmlSerializer для создания объекта, представляющего XML файл, и теперь я хочу добавить схему в rootelement моего xml файла. Я могу добавить пространства имен, подобные следующим
XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
System.IO.FileStream fs = new FileStream(@"C:\test.xml", FileMode.Create);
TextWriter writer = new StreamWriter(fs, new UTF8Encoding());
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("xy","http://www.w3.org/2005/08/addressing");
ns.Add("xlink","http://www.w3.org/1999/xlink");
serializer.Serialize(writer, myObject, ns);
Но как добавить атрибут xsi:schemalocation
к моему корневому элементу в моем коде С#. Пространство имен было добавлено простым ns.Add()
. Я хотел бы избежать беспорядка с созданным xsd.exe классом С#.
Или мне нужно вручную отредактировать созданный класс С# и добавить некоторый атрибут в корневой элемент моего xml?
EDIT: Я видел примеры, когда мне нужно вручную редактировать свой С#, но должен быть способ сделать это в коде!! Если мы можем добавить пространства имен в наш корневой элемент, почему бы не добавить добавления схем?