为什么在解析时会删除xml声明?

时间:2017-11-30 13:24:32

标签: c# xml

我尝试使用以下程序将一个节点值从一个xml文件修改为另一个xml文件,该程序从名为的文件夹中的xml文件中获取第一个节点 pub-title 的值 abc ,然后将该值粘贴到名为 xyz 的文件夹中的另一个xml文件中的第一个节点 publisher-name

注意:实现 escape_string 方法不会修改UTF-8实体值并保持原样。

var job_folders = Directory.EnumerateDirectories(textBox1.Text, "*", SearchOption.TopDirectoryOnly);
foreach (string job_folder in job_folders)
{
    var target_xml_file = Directory.GetFiles(job_folder, "*.xml", SearchOption.AllDirectories).Where(a => Path.GetFileName(Path.GetDirectoryName(x)).ToLower() == "abc").First();
    var target_meta_file = Directory.GetFiles(job_folder, "*.xml", SearchOption.AllDirectories).Where(a => Path.GetFileName(Path.GetDirectoryName(x)).ToLower() == "xyz").First();

    string path = Path.GetFullPath(target_meta_file);
    string file_content = escape_string(File.ReadAllText(path), 0);
    XDocument doc = XDocument.Parse(file_content, LoadOptions.PreserveWhitespace);
    var lbl=doc.Descendants("pub-title").First().Value;
    XDocument doc2 = XDocument.Parse(escape_string(File.ReadAllText(target_xml_file), 0), LoadOptions.PreserveWhitespace);
    doc2.DocumentType.InternalSubset = null;
    doc2.Descendants("publisher-name").First().Value=lbl;
    doc2.Save(target_xml_file);
    File.WriteAllText(target_xml_file, escape_string(doc2.ToString(), 1));
}

MessageBox.Show("Complete");

private static string escape_string(string input_string, int option)
{
    switch (option)
    {
        case 0:
            return input_string.Replace("&", "&").ToString();
        case 1:
            return input_string.Replace("&", "&").ToString();
        default:
            return null;
    }
}

一切顺利但<?xml version="1.0" encoding="utf-8"?>已从 target_xml_file 中的文件中删除。

我该如何解决这个问题? 修改前的文件

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="jats-html.xsl"?>
<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Publishing DTD with OASIS Tables v1.0 20120330//EN" "JATS-journalpublishing-oasis-article1.dtd"[]>
<article article-type="proceedings" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:oasis="http://www.niso.org/standards/z39-96/ns/oasis-exchange/table">
<front>
<journal-meta>
<journal-id journal-id-type="publisher-id" />
<journal-title-group>
<journal-title>Eleventh &#x0026; Tenth International Conference on Correlation Optics</journal-title>
</journal-title-group>
<issn pub-type="epub">0277-786X</issn>
<publisher>
<publisher-name>SPIE</publisher-name>
</publisher>
</journal-meta>
....
....

之后的文件
<?xml-stylesheet type="text/xsl" href="jats-html.xsl"?>
<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Publishing DTD with OASIS Tables v1.0 20120330//EN" "JATS-journalpublishing-oasis-article1.dtd">
<article article-type="proceedings" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:oasis="http://www.niso.org/standards/z39-96/ns/oasis-exchange/table">
<front>
<journal-meta>
<journal-id journal-id-type="publisher-id" />
<journal-title-group>
<journal-title>Eleventh &#x0026; Tenth International Conference on Correlation Optics</journal-title>
</journal-title-group>
<issn pub-type="epub">0277-786X</issn>
<publisher>
<publisher-name>a</publisher-name>
</publisher>
</journal-meta>

2 个答案:

答案 0 :(得分:1)

在回答XDocument.ToString() drops XML Encoding Tag后,您不应使用ToString方法,而是使用StringWriter

using (var stream = new MemoryStream())
{
    using (var writer = new XmlTextWriter(stream, Encoding.UTF8))
    {
        doc2.Save(writer);
    }
    string xml = escape_string(Encoding.UTF8.GetString(stream.ToArray()), 1);
    File.WriteAllBytes(target_xml_file, Encoding.UTF8.GetBytes(xml));
}

答案 1 :(得分:0)

为什么不在这个过程之后简单地添加一个def slicing(BilateralGrids, GuidanceMaps, InputImages): output_images = [] for i in range(batchSize): grid = BilateralGrids[i] guide = GuidanceMaps[i] input_img = InputImages[i] output = performPixelwiseComputation(grid, guide, input_img) #output.shape => (256, 256, 3) output_images.append(output) return output_images #output_images.shape => (BatchSize, 256, 256, 3) 方法,比如

XDeclaration

然后保存文件。它只需要两行代码。