Usuário com melhor resposta
Cabecalho xml

Pergunta
-
Pessoal,
Estou criando um documento xml pra enviar a um webservice mas qdo o documento é criado, eu nao tenho escrito na 1a linha:
<?xml version="1.0" encoding="utf-8"?>
Como faco isso ?
---- MEU DOCUMENTO ---
XDocument xmlDoc = new XDocument(
new XDeclaration("1.0", "ISO-8859-1", "true"),
new XElement("requisicao",
new XAttribute("id", "a93f-c2e9f612c293"),
new XAttribute("versao", "10"), ...
Respostas
-
O que quis dizer é pq tenho que usar o stringwriter e getstrigbuilder.
Eu nao poderia enviar para o webservice somente o xmlDoc ?
O retorno do objeto não é possível quando possui uma class XDeclaration, a mesma não pode ser serializada, veja estes exemplos:
[WebMethod] public string ReturnString() { XDocument xmlDoc = new XDocument(new XDeclaration("1.0", "ISO-8859-1", "true"), new XElement("requisicao", new XAttribute("id", "a93f-c2e9f612c293"), new XAttribute("versao", "10"))); var sw = new System.IO.StringWriter(); xmlDoc.Save(sw); return sw.GetStringBuilder().ToString(); } [WebMethod] public XmlDocument ReturnXmlDocument() { XDocument xmlDoc = new XDocument(new XDeclaration("1.0", "ISO-8859-1", "true"), new XElement("requisicao", new XAttribute("id", "a93f-c2e9f612c293"), new XAttribute("versao", "10"))); var sw = new System.IO.StringWriter(); xmlDoc.Save(sw); XmlDocument x = new XmlDocument(); x.LoadXml(sw.GetStringBuilder().ToString()); return x; } //Este método vai dar erro pois a classe XDeclaration não pode ser serializada, //descomente o código abaixo e faça o teste //[WebMethod] //public XDocument ReturnXDocument() //{ // XDocument xmlDoc = new XDocument(new XDeclaration("1.0", "ISO-8859-1", "true"), // new XElement("requisicao", // new XAttribute("id", "a93f-c2e9f612c293"), // new XAttribute("versao", "10"))); // return xmlDoc; //}
Vitor Mendes | Seu feedback é muito importante para todos!
Visite o meu site: http://www.vitormendes.com.br/- Marcado como Resposta AGA Neto segunda-feira, 11 de junho de 2012 06:10
- Editado Vitor Mendes segunda-feira, 11 de junho de 2012 11:03
Todas as Respostas
-
A declaração do XML só estera disponível depois que o método Save() for chamado, veja se isto te ajuda:
XDocument xmlDoc = new XDocument(new XDeclaration("1.0", "ISO-8859-1", "true"), new XElement("requisicao", new XAttribute("id", "a93f-c2e9f612c293"), new XAttribute("versao", "10"))); var sw = new StringWriter(); xmlDoc.Save(sw); string resultado = sw.GetStringBuilder().ToString();
Vitor Mendes | Seu feedback é muito importante para todos!
Visite o meu site: http://www.vitormendes.com.br/ -
-
Não sei se entendi muito bem sua pergunta, mas deve ter faltando você dar um using na bibliote System.IO:
using System.IO;
Ou pode chamar direto:
XDocument xmlDoc = new XDocument(new XDeclaration("1.0", "ISO-8859-1", "true"), new XElement("requisicao", new XAttribute("id", "a93f-c2e9f612c293"), new XAttribute("versao", "10"))); var sw = new System.IO.StringWriter(); xmlDoc.Save(sw); string resultado = sw.GetStringBuilder().ToString();
Vitor Mendes | Seu feedback é muito importante para todos!
Visite o meu site: http://www.vitormendes.com.br/ -
-
O que quis dizer é pq tenho que usar o stringwriter e getstrigbuilder.
Eu nao poderia enviar para o webservice somente o xmlDoc ?
O retorno do objeto não é possível quando possui uma class XDeclaration, a mesma não pode ser serializada, veja estes exemplos:
[WebMethod] public string ReturnString() { XDocument xmlDoc = new XDocument(new XDeclaration("1.0", "ISO-8859-1", "true"), new XElement("requisicao", new XAttribute("id", "a93f-c2e9f612c293"), new XAttribute("versao", "10"))); var sw = new System.IO.StringWriter(); xmlDoc.Save(sw); return sw.GetStringBuilder().ToString(); } [WebMethod] public XmlDocument ReturnXmlDocument() { XDocument xmlDoc = new XDocument(new XDeclaration("1.0", "ISO-8859-1", "true"), new XElement("requisicao", new XAttribute("id", "a93f-c2e9f612c293"), new XAttribute("versao", "10"))); var sw = new System.IO.StringWriter(); xmlDoc.Save(sw); XmlDocument x = new XmlDocument(); x.LoadXml(sw.GetStringBuilder().ToString()); return x; } //Este método vai dar erro pois a classe XDeclaration não pode ser serializada, //descomente o código abaixo e faça o teste //[WebMethod] //public XDocument ReturnXDocument() //{ // XDocument xmlDoc = new XDocument(new XDeclaration("1.0", "ISO-8859-1", "true"), // new XElement("requisicao", // new XAttribute("id", "a93f-c2e9f612c293"), // new XAttribute("versao", "10"))); // return xmlDoc; //}
Vitor Mendes | Seu feedback é muito importante para todos!
Visite o meu site: http://www.vitormendes.com.br/- Marcado como Resposta AGA Neto segunda-feira, 11 de junho de 2012 06:10
- Editado Vitor Mendes segunda-feira, 11 de junho de 2012 11:03