5

Please view following XML namespace and schemaLocation.

<agr:ABWInvoice 
  xsi:schemaLocation = "
    http://services.agresso.com/schema/ABWInvoice/2011/11/14 
    http://services.agresso.com/schema/ABWInvoice/2011/11/14/ABWInvoice.xsd" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:agrlib = "http://services.agresso.com/schema/ABWSchemaLib/2011/11/14"
  xmlns:agr = "http://services.agresso.com/schema/ABWInvoice/2011/11/14"
>

</agr:ABWInvoice>

I have added namespaces in following way, which seems working fine:

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
ns.Add("agrlib", "http://services.agresso.com/schema/ABWSchemaLib/2011/11/14");
ns.Add("agr", "http://services.agresso.com/schema/ABWInvoice/2011/11/14");

But, how to add following schemalocation? Any ideas?

xsi:schemaLocation="http://services.agresso.com/schema/ABWInvoice/2011/11/14 http://services.agresso.com/schema/ABWInvoice/2011/11/14/ABWInvoice.xsd"
John Saunders
  • 160,644
  • 26
  • 247
  • 397
mrd
  • 2,095
  • 6
  • 23
  • 48

4 Answers4

7
xsi:schemaLocation="..."

is not a namespace declaration: it's an attribute (whose value happens to be a namespace, but never mind that). So you would add it with a method that sets an attribute value. I'm not familiar with the C# XML API, but it's probably something like this:

XmlElement.SetAttributeValue (localname, prefix, namespace, value)

localname should be "schemaLocation"
prefix = "xsi"
namespace = "http://www.w3.org/2001/XMLSchema-instance"
value = "your schema location"

Gabrielius
  • 1,045
  • 12
  • 18
Mike Sokolov
  • 6,914
  • 2
  • 23
  • 31
  • Thanks Mike. I did as following my answer. – mrd Jan 29 '13 at 08:25
  • For C# you can use for example the XmlDocument class, which has a method CreateAtrribute(prefix, localname, namespaceUri). It worked exactly as explained by Mike. – mFritz Dec 16 '21 at 09:54
7

The reply of Mike lead me to get following answer:

    [XmlAttributeAttribute("schemaLocation", AttributeName = "schemaLocation", 
    Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string SchemaLocation = "http://services.agresso.com/schema/ABWInvoice/2011/11/14 http://services.agresso.com/schema/ABWInvoice/2011/11/14/ABWInvoice.xsd";
mrd
  • 2,095
  • 6
  • 23
  • 48
1

Just add this code in your class

public partial class MyClass{

    [XmlAttribute(Namespace = System.Xml.Schema.XmlSchema.InstanceNamespace)]
    public string schemaLocation = "http://www.adap.cx/m3/x4 lksdjv45.xsd";

... }

Nayan Hodar
  • 508
  • 7
  • 12
0

For me the accepted answer did not work, but this did work:

    var schema = new XmlSchema();
    schema.Namespaces.Add("xmlns", "urn:oasis:names:specification:ubl:schema:xsd:OrderResponse-2");
    var doc = new XmlDocument();
    doc.Schemas.Add(schema);
    var rootElement = doc.CreateElement("MyRoot");
    rootElement.SetAttribute("xmlns", "urn:oasis:names:specification:ubl:schema:xsd:OrderResponse-2");
    rootElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    rootElement.SetAttribute("schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "urn:oasis:names:specification:ubl:schema:xsd:OrderResponse-2 file:///C:/DIGITALCAB/DigiTaxi/schema/OIOUBL/maindoc/UBL-OrderResponse-2.0.xsd");
Gabrielius
  • 1,045
  • 12
  • 18