How to Add schemaLocation attribute to an XML document

落花浮王杯 提交于 2020-12-06 04:15:53

问题


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"

回答1:


xs: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

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

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




回答2:


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";



回答3:


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";

... }



来源:https://stackoverflow.com/questions/14562975/how-to-add-schemalocation-attribute-to-an-xml-document

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!