c#XML文件编写器

时间:2017-04-19 16:27:21

标签: c# xml

我有一个XML文件,格式如下: 我正在尝试创建完全相同的文件,但内容不同。 我尝试了几个选项,但我根本无法在XML标头之前写入以保持文件的格式。

有人可以帮忙吗?

valObj

这是最新的,无效的代码:

<MemoQResource ResourceType="NonTrans" Version="1.0">
 <Resource>
  <Guid>8380bf45-86cc-4c18-8ce3-11d69746d630</Guid>
  <FileName>do.txt</FileName>
  <Name>do</Name>
  <Description />
 </Resource>
</MemoQResource>
<?xml version="1.0" encoding="utf-8"?>
 <nonTrans version="1.0">
 <nonTransRule>123456</nonTransRule>
 <nonTransRule>123456</nonTransRule>
 <nonTransRule>123456</nonTransRule>
 <nonTransRule>123456</nonTransRule>
</nonTrans>

2 个答案:

答案 0 :(得分:0)

这不是有效的XML - XMLWriter不会产生它。 XML声明需要go first在文件中。

  

XML声明通常显示为XML文档中的第一行。不需要XML声明,但是,如果使用它,它必须是文档中的第一行,并且在它之前不能有其他内容或空格。

如果您确实需要生成这个,请创建两个XML编写器,一个用于顶部文档而不用声明,另一个用于底部文档,带有1.0声明。然后,使用您选择的文件串联方法将两个文件合并在一起。类似(未经测试),File.AppendAllText("file1.xml", File.ReadAllText("file2.xml"));

ReadAllText

AppendAllText

答案 1 :(得分:0)

这些只是代码的示例,可以帮助您开始将两个XML文档附加在一起 ​​- 它应该根据需要进行优化/调整。

它显示了一种方法,使用MemoryStream将2个XML片段组合在内存中,然后一次性将其写入文件。

它显示了另一种方法,只是写入单个文件,然后重新打开它并将第二个块附加到最后。

毫无疑问,根据您的使用情况,您可以使用其他排列,例如创建了2个临时文件,然后将其读回,并写入第3个文件。

如果要避免磁盘上的部分正确/临时文件,并且包含小型XML文档,请仅使用MemoryStream

第一个示例 - 使用单个MemoryStream,然后将两个部分写入流<​​/ h2>
private bool ConvertFile(string infile, string outfile)
{
    Guid g = Guid.NewGuid();

    string filename = Path.GetFileName(infile);
    string name = Path.GetFileNameWithoutExtension(infile);
    string description = "";
    string nontransrule = "123456\r\n123456\r\n123456\r\n123456";

    using (MemoryStream ms = new MemoryStream())
    {
        XmlTextWriter writer0 = new XmlTextWriter(ms, Encoding.UTF8);
        writer0.Formatting = Formatting.Indented;
        writer0.WriteStartElement("MemoQResource");
        writer0.WriteAttributeString("ResourceType", "NonTrans");
        writer0.WriteAttributeString("Version", "1.0");
        writer0.WriteStartElement("Resource");
        writer0.WriteElementString("Guid", Convert.ToString(g));
        writer0.WriteElementString("FileName", filename);
        writer0.WriteElementString("Name", name);
        writer0.WriteElementString("Description", description);
        writer0.WriteEndElement();
        writer0.WriteEndElement();

        writer0.Flush();

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.IndentChars = "\t";

        using (StreamWriter sr = new StreamWriter(ms, Encoding.UTF8))
        {
            sr.WriteLine();

            XmlWriter writer = XmlWriter.Create(sr, settings);
            writer.WriteStartDocument();
            writer.WriteStartElement("nonTrans");
            writer.WriteAttributeString("Version", "1.0");

            string[] words = nontransrule.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            foreach (string word in words)
            {
                writer.WriteElementString("nonTransRule", word);
            }

            writer.WriteEndElement();

            writer.Flush();

            using (FileStream fs = new FileStream(outfile, FileMode.Create))
            {
                ms.Seek(0, SeekOrigin.Begin);
                ms.WriteTo(fs);
                fs.Close();
            }
        }
    }

    return false;
}

private void button1_Click(object sender, EventArgs e)
{
    ConvertFile("dot.txt", "S:\\test.xml");
}

第二个示例 - 获取XmlTextWriter以编写第一部分,然后以附加模式打开文件以执行第二部分

只需写入同一个文件并附加:

即可
private bool ConvertFile(string infile, string outfile)
{
    Guid g = Guid.NewGuid();

    string filename = Path.GetFileName(infile);
    string name = Path.GetFileNameWithoutExtension(infile);
    string description = "";
    string nontransrule = "123456\r\n123456\r\n123456\r\n123456";

    using (FileStream fs = new FileStream(outfile, FileMode.Create))
    {
        XmlTextWriter writer0 = new XmlTextWriter(fs, Encoding.UTF8);
        writer0.Formatting = Formatting.Indented;
        writer0.WriteStartElement("MemoQResource");
        writer0.WriteAttributeString("ResourceType", "NonTrans");
        writer0.WriteAttributeString("Version", "1.0");
        writer0.WriteStartElement("Resource");
        writer0.WriteElementString("Guid", Convert.ToString(g));
        writer0.WriteElementString("FileName", filename);
        writer0.WriteElementString("Name", name);
        writer0.WriteElementString("Description", description);
        writer0.WriteEndElement();
        writer0.WriteEndElement();

        writer0.Flush();
    }

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.IndentChars = "\t";

    using (StreamWriter sr = new StreamWriter(outfile, true, Encoding.UTF8))
    {
        sr.WriteLine();

        using (XmlWriter writer = XmlWriter.Create(sr, settings))
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("nonTrans");
            writer.WriteAttributeString("Version", "1.0");

            string[] words = nontransrule.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            foreach (string word in words)
            {
                writer.WriteElementString("nonTransRule", word);
            }

            writer.WriteEndElement();

            writer.Flush();
        }
    }

    return false;
}

示例输出:

<MemoQResource ResourceType="NonTrans" Version="1.0">
  <Resource>
    <Guid>0c5beea5-f892-47f0-9898-247d5ded3d1a</Guid>
    <FileName>dot.txt</FileName>
    <Name>dot</Name>
    <Description />
  </Resource>
</MemoQResource>
<?xml version="1.0" encoding="utf-8"?>
<nonTrans Version="1.0">
    <nonTransRule>123456</nonTransRule>
    <nonTransRule>123456</nonTransRule>
    <nonTransRule>123456</nonTransRule>
    <nonTransRule>123456</nonTransRule>
</nonTrans>