Подтвердить что ты не робот

Как добавить xsi schemalocation в корневой объект С# XmlSerializer

Я использую 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: Я видел примеры, когда мне нужно вручную редактировать свой С#, но должен быть способ сделать это в коде!! Если мы можем добавить пространства имен в наш корневой элемент, почему бы не добавить добавления схем?

4b9b3361

Ответ 1

Предположим, что следующий XSD:

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" xmlns="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="elementB">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="FirstName" type="xsd:string"/>
                <xsd:element name="LastName" type="xsd:string"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>  
</xsd:schema>

Есть два способа, по крайней мере, сделать это. Первый основан на наследовании и том, как вы можете играть с аннотациями сериализатора.

xsd.exe генерирует это:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18034
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.0.30319.1.
// 

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://tempuri.org/XMLSchema.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/XMLSchema.xsd", IsNullable=false)]
public partial class elementB {

    private string firstNameField;

    private string lastNameField;

    /// <remarks/>
    public string FirstName {
        get {
            return this.firstNameField;
        }
        set {
            this.firstNameField = value;
        }
    }

    /// <remarks/>
    public string LastName {
        get {
            return this.lastNameField;
        }
        set {
            this.lastNameField = value;
        }
    }
}

Чтобы "ввести" xsi:schemaLocation добавить новый класс, elementA : elementB; обратите внимание:

  • Настройка System.Xml.Serialization.XmlRootAttribute
  • schemaLocation настройка свойств.

Программа тестирования:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            elementB b = new elementB();
            b.FirstName = "P";
            b.LastName = "G";

            XmlSerializer ser = new XmlSerializer(typeof(elementB));
            StringBuilder sb = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(sb, new XmlWriterSettings() { Indent = true })) 
            {
                ser.Serialize(writer, b);
            }
            Console.WriteLine(sb.ToString());

            elementA a = new elementA();
            a.FirstName = "P";
            a.LastName = "G";
            a.schemaLocation = "http://tempuri.org/XMLSchema.xsd me";
            ser = new XmlSerializer(typeof(elementA));
            sb = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(sb, new XmlWriterSettings() { Indent = true }))
            {
                ser.Serialize(writer, a);
            }
            Console.WriteLine(sb.ToString());
        }
    }
}

[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/XMLSchema.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/XMLSchema.xsd", ElementName = "elementB", IsNullable = false)]
public partial class elementA : elementB
{

    private string torefField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string schemaLocation
    {
        get
        {
            return this.torefField;
        }
        set
        {
            this.torefField = value;
        }
    }
}

Генерирует ожидаемый результат:

<?xml version="1.0" encoding="utf-16"?>
<elementB xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/XMLSchema.xsd">
  <FirstName>P</FirstName>
  <LastName>G</LastName>
</elementB>
<?xml version="1.0" encoding="utf-16"?>
<elementB xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="http://tempuri.org/XMLSchema.xsd me" xmlns="http://tempuri.org/XMLSchema.xsd">
  <FirstName>Petru</FirstName>
  <LastName>Gardea</LastName>
</elementB>

Второй способ зависит от пользовательского сценария, который будет вводить то, что вы хотите, где бы вы ни хотели (предполагая соответствующую логику).

Вы реализуете пользовательский XmlWriter:

class MyXmlWriter : XmlWriter
{
    XmlWriter _writer;
    bool _docElement = true;

    public string SchemaLocation { get; set; }
    public string NoNamespaceSchemaLocation { get; set; }

    public MyXmlWriter(XmlWriter writer)
    {
        _writer = writer;
    }

    (other methods omitted)

    public override void WriteStartElement(string prefix, string localName, string ns)
    {
        _writer.WriteStartElement(prefix, localName, ns);
        if (_docElement)
        {
            if (!string.IsNullOrEmpty(SchemaLocation))
            {
                _writer.WriteAttributeString("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", SchemaLocation);
            }
            if (!string.IsNullOrEmpty(NoNamespaceSchemaLocation))
            {
                _writer.WriteAttributeString("xsi", "noNamesapceSchemaLocation", "http://www.w3.org/2001/XMLSchema-instance", NoNamespaceSchemaLocation);
            }
            _docElement = false;
        }
    }

    (other methods omitted)

}

Измененная тестовая программа:

static void Main(string[] args)
{
    elementB b = new elementB();
    b.FirstName = "P";
    b.LastName = "G";

    XmlSerializer ser = new XmlSerializer(typeof(elementB));
    StringBuilder sb = new StringBuilder();
    using (XmlWriter writer = XmlWriter.Create(sb, new XmlWriterSettings() { Indent = true })) 
    {
        ser.Serialize(writer, b);
    }
    Console.WriteLine(sb.ToString());

    sb = new StringBuilder();

    using (XmlWriter writer = XmlWriter.Create(sb, new XmlWriterSettings() { Indent = true }))
    {
        MyXmlWriter newWriter = new MyXmlWriter(writer) { SchemaLocation = "http://tempuri.org/XMLSchema.xsd me" };
        ser.Serialize(newWriter, b);
    }
    Console.WriteLine(sb.ToString());
}

Результат тот же...

<?xml version="1.0" encoding="utf-16"?>
<elementB xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/XMLSchema.xsd">
  <FirstName>P</FirstName>
  <LastName>G</LastName>
</elementB>
<?xml version="1.0" encoding="utf-16"?>
<elementB xsi:schemaLocation="http://tempuri.org/XMLSchema.xsd me" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
  <FirstName>P</FirstName>
  <LastName>G</LastName>
</elementB>

Ответ 2

XSD.exe генерирует частичные классы, поэтому вы можете добавить свой собственный отдельный частичный класс для хранения таких вещей, как xsi: schemaLocation, как поля или свойства.

Итак, добавляя к классу elementBB класса @Petru Gardea, вам нужно только создать другой файл в вашем проекте и добавить этот частичный класс:

public partial class elementB 
{
    [XmlAttributeAttribute("schemaLocation", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
    public string xsiSchemaLocation = "http://www.acme.com/xml/OrderXML-1-0.xsd";
}

Есть одна проблема, с которой я столкнулся с этим, и по умолчанию xsd.exe не добавляет пространство имен в сгенерированный файл (ы). Когда вы создадите этот собственный частичный класс, он скорее всего будет находиться в пространстве имен. Поскольку < default namespace > и явно определенное пространство имен не совпадают, частичное не будет работать. Таким образом, вам нужно использовать опцию namespace на xsd.exe, чтобы фактически получить сгенерированные классы в ваше пространство имен.