4

请查看以下 XML 命名空间和 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>

我以以下方式添加了命名空间,这似乎工作正常:

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

但是,如何添加以下架构位置?有任何想法吗?

xsi:schemaLocation="http://services.agresso.com/schema/ABWInvoice/2011/11/14 http://services.agresso.com/schema/ABWInvoice/2011/11/14/ABWInvoice.xsd"
4

3 回答 3

7
xs:schemaLocation="..."

不是命名空间声明:它是一个属性(其值恰好是命名空间,但没关系)。因此,您可以使用设置属性值的方法来添加它。我不熟悉 C# XML API,但它可能类似于

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

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

于 2013-01-29T02:36:47.627 回答
6

Mike 的回复使我得到以下答案:

    [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";
于 2013-01-29T08:27:44.680 回答
1

只需在您的课程中添加此代码

公共部分类 MyClass{

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

... }

于 2016-12-06T11:09:50.167 回答