質問

I need to generate an XML file using C# class (XmlDocument or XDocument) with the following root element:

<ns1:ConsultaSeqRps xmlns:ns1="http://localhost:8080/WsNFe2/lote" 
    xmlns:tipos="http://localhost:8080/WsNFe2/tp" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://localhost:8080/WsNFe2/lote http://localhost:8080/WsNFe2/xsd/ConsultaSeqRps.xsd">

I've tried various alternatives using setAttribute and XmlNamespaceManager but without success.

役に立ちましたか?

解決

It's pretty straightforward, except maybe for the use of XAttribute to add a named namespace:

XNamespace ns1 = "http://localhost:8080/WsNFe2/lote";
XNamespace tipos = "http://localhost:8080/WsNFe2/tp";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";

var doc = new XElement(ns1 + "ConsultaSeqRps",
    new XAttribute(XNamespace.Xmlns + "ns1", ns1), 
    new XAttribute(XNamespace.Xmlns + "tipos", tipos), 
    new XAttribute(XNamespace.Xmlns + "xsi", xsi),
    new XAttribute(xsi + "schemaLocation", 
      "http://localhost:8080/WsNFe2/lote http://localhost:8080/WsNFe2/xsd/ConsultaSeqRps.xsd")
    );

他のヒント

You can take the advantage of LINQ to XML, the following article will help you to Create namespace in c# How to: Create a Document with Namespaces (C#) (LINQ to XML)

Hope that help.

Regards

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top