【发布时间】:2017-11-30 13:24:31
【问题描述】:
我正在尝试使用以下程序将某个节点值从一个 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 & 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 & 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>
【问题讨论】:
-
为什么要保存文件两次,第二次保存为 tekst 而不是 xml 文件?如果在
doc2.Save(target_xml_file);之后删除File.WriteAllText(target_xml_file, escape_string(doc2.ToString(), 1));会发生什么? -
XDocument.ToString()不包括 XML 声明。它在.Declaration属性中明确可用。 -
@JeroenMostert 那我该怎么办?
-
@oerkelens 如果我删除
File.WriteAllText(target_xml_file, escape_string(doc2.ToString(), 1));,那么像&amp;#x2014;这样的UTF-8 代码会像&amp;#x2014;一样保留。我需要使用escape_string 方法将其恢复为&amp;#x2014;跨度> -
但是你为什么还要尝试
doc2.Save(target_xml_file);呢?您是否查看过之后会发生什么,或者您是否只是在第一次将文件保存为 XML 文档后才尝试再次覆盖该文件?