以下のプログラムを使用して、あるxmlファイルから別のノード値を変更しようとしています。このプログラムは、abcというフォルダー内のxmlファイルから最初のノードpub-titleから値を取得し、その値を最初のノードパブリッシャーに貼り付けます-名前という名前のフォルダ内の別のXMLファイルにXYZ。
注: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>
XDocument.ToString()への回答に続いてXMLエンコーディングタグを削除します。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));
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加