C# 用C语言导出XML#

C# 用C语言导出XML#,c#,xml,export,C#,Xml,Export,我正在尝试从C语言中的一些数据导出xml# 保存XML后,我发现XML包含 <?xml version="1.0" encoding="utf-8"?> 我根本没有进入,导致了如下问题 <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="..\..\dco.xsl"?> <S> <B> </B> </S

我正在尝试从C语言中的一些数据导出xml#

保存XML后,我发现XML包含

<?xml version="1.0" encoding="utf-8"?>

我根本没有进入,导致了如下问题

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="..\..\dco.xsl"?>
<S>
  <B>
  </B>
</S>

我不想第一排出现,有什么想法吗?
感谢您的回复。

如果我理解正确,您需要一个没有标题的XML文件。看一看


基本上,您需要初始化和类,然后调用
doc.Save(writer)

如果我理解正确,您需要一个没有标题的XML文件。看一看

基本上,您需要初始化和类,然后调用
doc.Save(writer)

它的PI(处理指令),它是处理xml文件时所需的并且包含重要信息

在编写xml文件时尝试以下操作:

XmlWriter w;
w.Settings = new XmlWriterSettings();
w.Settings.ConformanceLevel = ConformanceLevel.Fragment;
w.Settings.OmitXmlDeclaration = true;

它的PI(处理指令)是处理xml文件时所需的并包含重要信息

在编写xml文件时尝试以下操作:

XmlWriter w;
w.Settings = new XmlWriterSettings();
w.Settings.ConformanceLevel = ConformanceLevel.Fragment;
w.Settings.OmitXmlDeclaration = true;

您所说的是“在”您用字符串解析后,正如您在上面看到的,您的结果包含重复声明吗

现在我不确定您是如何保存响应的,但这里有一个示例应用程序,它会产生类似的结果

XDocument doc = XDocument.Parse("<?xml-stylesheet type=\"text/xsl\" href=\"dco.xsl\"?><S><B></B></S>");
            doc.Save(Console.OpenStandardOutput());
XDocument doc=XDocument.Parse(“”);
doc.Save(Console.OpenStandardOutput());
生成结果:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="dco.xsl"?>
<S>
  <B></B>
</S>

这就是你的问题所在。保存前需要删除xml声明。这可以通过在保存xml输出时使用xml编写器来完成。下面是一个示例应用程序,它使用扩展方法编写新文档,而无需声明

class Program
    {
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Parse("<?xml-stylesheet type=\"text/xsl\" href=\"dco.xsl\"?><S><B></B></S>");
            doc.SaveWithoutDeclaration(Console.OpenStandardOutput());
            Console.ReadKey();
        }


    }

    internal static class Extensions
    {
        public static void SaveWithoutDeclaration(this XDocument doc, string FileName)
        {
            using(var fs = new StreamWriter(FileName))
            {
                fs.Write(doc.ToString());
            }
        }

        public static void SaveWithoutDeclaration(this XDocument doc, Stream Stream)
        {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(doc.ToString());
            Stream.Write(bytes, 0, bytes.Length);
        }
    }
类程序
{
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Parse(“”);
doc.savewithout声明(Console.OpenStandardOutput());
Console.ReadKey();
}
}
内部静态类扩展
{
公共静态void savewithout声明(此XDocument文档,字符串文件名)
{
使用(var fs=newstreamwriter(文件名))
{
fs.Write(doc.ToString());
}
}
公共静态void savewithout声明(此XDocument文档,流)
{
byte[]bytes=System.Text.Encoding.UTF8.GetBytes(doc.ToString());
Stream.Write(字节,0,字节.长度);
}
}
您所说的是“在”您用字符串解析后,正如您在上面看到的,您的结果包含重复声明吗

现在我不确定您是如何保存响应的,但这里有一个示例应用程序,它会产生类似的结果

XDocument doc = XDocument.Parse("<?xml-stylesheet type=\"text/xsl\" href=\"dco.xsl\"?><S><B></B></S>");
            doc.Save(Console.OpenStandardOutput());
XDocument doc=XDocument.Parse(“”);
doc.Save(Console.OpenStandardOutput());
生成结果:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="dco.xsl"?>
<S>
  <B></B>
</S>

这就是你的问题所在。保存前需要删除xml声明。这可以通过在保存xml输出时使用xml编写器来完成。下面是一个示例应用程序,它使用扩展方法编写新文档,而无需声明

class Program
    {
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Parse("<?xml-stylesheet type=\"text/xsl\" href=\"dco.xsl\"?><S><B></B></S>");
            doc.SaveWithoutDeclaration(Console.OpenStandardOutput());
            Console.ReadKey();
        }


    }

    internal static class Extensions
    {
        public static void SaveWithoutDeclaration(this XDocument doc, string FileName)
        {
            using(var fs = new StreamWriter(FileName))
            {
                fs.Write(doc.ToString());
            }
        }

        public static void SaveWithoutDeclaration(this XDocument doc, Stream Stream)
        {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(doc.ToString());
            Stream.Write(bytes, 0, bytes.Length);
        }
    }
类程序
{
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Parse(“”);
doc.savewithout声明(Console.OpenStandardOutput());
Console.ReadKey();
}
}
内部静态类扩展
{
公共静态void savewithout声明(此XDocument文档,字符串文件名)
{
使用(var fs=newstreamwriter(文件名))
{
fs.Write(doc.ToString());
}
}
公共静态void savewithout声明(此XDocument文档,流)
{
byte[]bytes=System.Text.Encoding.UTF8.GetBytes(doc.ToString());
Stream.Write(字节,0,字节.长度);
}
}
您尝试过LINQ到XML吗?您尝试过LINQ到XML吗?