Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何添加XML声明?_C#_.net_Xml_Wcf_Declaration - Fatal编程技术网

C# 如何添加XML声明?

C# 如何添加XML声明?,c#,.net,xml,wcf,declaration,C#,.net,Xml,Wcf,Declaration,我有来自URL的Xml输出 使用httpget方法 如何添加此XML声明 ?xml version=“1.0”encoding=“ISO-8859-1”?> 这是服务合同 [ServiceContract] public interface IService1 { [OperationContract] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml,

我有来自URL的Xml输出 使用httpget方法

如何添加此XML声明

?xml version=“1.0”encoding=“ISO-8859-1”?>

这是服务合同

[ServiceContract]
public interface IService1
{
    [OperationContract]
          [WebInvoke(Method = "GET",
          ResponseFormat = WebMessageFormat.Xml,
          //BodyStyle = WebMessageBodyStyle.Wrapped,
          UriTemplate = "{id}")]

    XElement GetRecipesByID(string id);
这是执行,, 我将DataTable转换为Xml并从URL获取数据

public XElement GetRecipesByID(string id)
    {
        StringWriter str = new StringWriter();
        DataSet dataSet = new DataSet();
        dataSet.Tables.Add(Table);
        XmlTextWriter xtext = new XmlTextWriter(str);
        xtext.Formatting = Formatting.Indented;
        xtext.WriteStartDocument();
            xtext.WriteStartElement("rss");
            xtext.WriteAttributeString("version", "2.0");
            xtext.WriteStartElement("channel");
            xtext.WriteElementString("title", "GetCard");
            xtext.WriteElementString("link", "10.0.0.253");
            xtext.WriteElementString("lastBuildDate", " ");
            xtext.WriteElementString("generator", "Alikas Feed");
            xtext.WriteElementString("error", error);
            xtext.WriteElementString("cardid", "RX0016502");
            xtext.WriteElementString("name", " ");
            xtext.WriteElementString("passport_id", "60001082881");
            xtext.WriteElementString("tel", " ");
            xtext.WriteEndElement();
            xtext.WriteEndDocument();

            result = str.ToString();

        xtext.Close();

        XmlDocument doc = new XmlDocument();
        doc.Save(str);


        return XElement.Parse(result);

    }
当我在文件中保存这个xml时,会有xml声明,但当我尝试从URL获取这个xml时,xml声明不会显示。 我需要从URL显示此声明。
谢谢

WriteStartDocument
应该已经在添加XML声明了。它使用您的
文本编写器指定的
编码

您使用的是
StringWriter
,它将始终报告UTF-16,因为.NET字符串在UTF-16中。要获得ISO-8859-1,您需要构造一个带有显式编码的
StreamWriter
或其他
TextWriter

您是否考虑过直接创建
XDocument
?在这种情况下,无需使用
XmlWriter

尝试以下操作:

            string header = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><rss version=\"2.0\"/>";
            XDocument doc = XDocument.Parse(header);

            XElement rss = doc.Root;

            Boolean error = false;
            rss.Add(new XElement("channel", new object[] {
                new XElement("title", "GetCard"),
                new XElement("link", "10.0.0.253"),
                new XElement("lastBuildDate", " "),
                new XElement("generator", "Alikas Feed"),
                new XElement("error", error),
                new XElement("cardid", "RX0016502"),
                new XElement("name"),
                new XElement("passport_id", "60001082881"),
                new XElement("tel")
            }));

            doc.Save(@"c:\temp\test.xml");
字符串头=”;
XDocument doc=XDocument.Parse(标题);
XElement rss=doc.Root;
布尔错误=假;
添加(新元素(“频道”,新对象[]){
新XElement(“标题”、“获取卡片”),
新XElement(“链接”,“10.0.0.253”),
新的XElement(“lastBuildDate”和“),
新XElement(“发电机”、“Alikas馈电”),
新元素(“错误”,错误),
新XElement(“cardid”、“RX0016502”),
新XElement(“名称”),
新条款(“护照id”、“60001082881”),
新XElement(“电话”)
}));
doc.Save(@“c:\temp\test.xml”);

有一种特殊的方法:

var xmlDoc = new XmlDocument();
xmlDoc.CreateXmlDeclaration("1.0", "ISO-8859-1", null);
xmlDoc.Save(str);

msdn on encoding参数中的注释:这是将XmlDocument保存到文件或流时使用的编码;因此,必须将其设置为编码类支持的字符串,否则保存失败。

这似乎对我有效

XDocument xel = XDocument.Parse(  "<root><el>123</el></root>" );
xel.Declaration = new XDeclaration( "1.0","UTF-8","true" );
xel.Save( @"c:\temp.xml" );
XDocument xel=XDocument.Parse(“123”);
xel.Declaration=新的XDecaration(“1.0”、“UTF-8”、“真”);
xel.Save(@“c:\temp.xml”);

是的,我尝试使用XDocument,但结果相同。当我在文件中写入这个xml时,我有xml声明,但在Chrome中没有显示。如果它在文件中,它就在文件中,所以听起来好像这不是代码问题。Chrome只是没有显示出来。