如何将schemaLocation属性添加到XML文档

mrd*_*mrd 2 c# xml xsd

请查看以下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>
Run Code Online (Sandbox Code Playgroud)

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

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");
Run Code Online (Sandbox Code Playgroud)

但是,如何添加以下schemalocation?有任何想法吗?

xsi:schemaLocation="http://services.agresso.com/schema/ABWInvoice/2011/11/14 http://services.agresso.com/schema/ABWInvoice/2011/11/14/ABWInvoice.xsd"
Run Code Online (Sandbox Code Playgroud)

Mik*_*lov 5

xs:schemaLocation="..."
Run Code Online (Sandbox Code Playgroud)

不是名称空间声明:它是一个属性(其值恰好是名称空间,但不要紧)。因此,您可以使用设置属性值的方法来添加它。我对C#XML API不熟悉,但是可能有点像

XmlElement.SetAttributeValue (localname, prefix, namespace, value)
Run Code Online (Sandbox Code Playgroud)

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


mrd*_*mrd 5

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

    [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";
Run Code Online (Sandbox Code Playgroud)