在使用修改文件后,如何在xml文件中保留十六进制字符
XDocument
?
例如,这是我在任何操作之前的文件
<?xml version="1.0" encoding="UTF-8"?>
<!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 International Conference on Correlation Optics</journal-title>
</journal-title-group>
<issn pub-type="epub">0277-786X</issn>
<publisher>
<publisher-name>SP×E</publisher-name>
</publisher>
</journal-meta>
<article-meta>
<article-id pub-id-type="doi">10.1117/12.2053541</article-id>
<title-group>
<article-title>Macro- and microscopic spectral-polarization characteristics of the structure of normal and abnormally located chordae tendianeae of left ventricular</article-title>
</title-group>
<contrib-group>
<contrib contrib-type="author">
<name>
<surname>Malyk</surname>
<given-names>Yu.Yu.</given-names>
</name>
<xref ref-type="aff" rid="a1"><sup>a</sup></xref>
</contrib>
<contrib contrib-type="author" corresp="yes">
<name>
<surname>Prydij</surname>
<given-names>O.G.</given-names>
</name>
<xref ref-type="aff" rid="a2"><sup>b</sup></xref>
<xref ref-type="corresp" rid="cor1">*</xref>
</contrib>
<contrib contrib-type="author">
<name>
<surname>Zymnyakov</surname>
<given-names>D.A.</given-names>
</name>
<xref ref-type="aff" rid="a3"><sup>c</sup></xref>
</contrib>
<contrib contrib-type="author">
<name>
<surname>Alonova</surname>
<given-names>M.V.</given-names>
</name>
<xref ref-type="aff" rid="a3"><sup>c</sup></xref>
</contrib>
<contrib contrib-type="author">
<name>
<surname>Ushakova</surname>
<given-names>O.V.</given-names>
</name>
<xref ref-type="aff" rid="a3"><sup>c</sup></xref>
</contrib>
<aff id="a1"><label><sup>a</sup></label>Bukovinian State Medical University, 2 Theatre Sq., Chernivtsi, 58000, Ukraine</aff>
<aff id="a2"><label><sup>b</sup></label>Chernivtsi National University, 2 Kotsyubinsky Str., Chernivtsi, 58012, Ukraine</aff>
<aff id="a3"><label><sup>c</sup></label>Saratov State Technical University, Polytechnicheskaya st., 77, Saratov, 410054, Russia</aff>
</contrib-group>
</article-meta>
</front>
</article>
这是在使用修改后
塞利安语
<!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 International Conference on Correlation Optics</journal-title>
</journal-title-group>
<issn pub-type="epub">0277-786X</issn>
<publisher>
<publisher-name>SP×E</publisher-name>
</publisher>
</journal-meta>
<article-meta>
<article-id pub-id-type="doi">10.1117/12.2053541</article-id>
<title-group>
<article-title>Macro- and microscopic spectral-polarization characteristics of the structure of normal and abnormally located chordae tendianeae of left ventricular</article-title>
</title-group>
<contrib-group>
<contrib contrib-type="author">
<name>
</name>
<xref ref-type="aff" rid="a1"><sup>a</sup></xref>
</contrib>
<contrib contrib-type="author" corresp="yes">
<name>
</name>
<xref ref-type="aff" rid="a2"><sup>b</sup></xref>
<xref ref-type="corresp" rid="cor1">*</xref>
</contrib>
<contrib contrib-type="author">
<name>
</name>
<xref ref-type="aff" rid="a3"><sup>c</sup></xref>
</contrib>
<contrib contrib-type="author">
<name>
</name>
<xref ref-type="aff" rid="a3"><sup>c</sup></xref>
</contrib>
<contrib contrib-type="author">
<name>
</name>
<xref ref-type="aff" rid="a3"><sup>c</sup></xref>
</contrib>
<aff id="a1"><label><sup>a</sup></label>Bukovinian State Medical University, 2 Theatre Sq., Chernivtsi, 58000, Ukraine</aff>
<aff id="a2"><label><sup>b</sup></label>Chernivtsi National University, 2 Kotsyubinsky Str., Chernivtsi, 58012, Ukraine</aff>
<aff id="a3"><label><sup>c</sup></label>Saratov State Technical University, Polytechnicheskaya st., 77, Saratov, 410054, Russia</aff>
</contrib-group>
</article-meta>
</front>
</article>
程序代码
void Button1Click(object sender, EventArgs e)
{
var basePath = textBox1.Text;
string[] filesindirectory = Directory.GetFiles(basePath, "*.xml",SearchOption.AllDirectories);
foreach (string fp in filesindirectory)
{
string file_content = escape_string(File.ReadAllText(fp), 0);
XDocument doc = XDocument.Parse(file_content, LoadOptions.PreserveWhitespace);
doc.Descendants("name").Elements().Remove();
File.WriteAllText(fp, escape_string(doc.ToString(), 1).ToString());
}
MessageBox.Show("Done");
}
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"?>
已删除,并且
<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Publishing DTD with OASIS Tables v1.0 20120330//EN" "JATS-journalpublishing-oasis-article1.dtd">
<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Publishing DTD with OASIS Tables v1.0 20120330//EN" "JATS-journalpublishing-oasis-article1.dtd"[]>
.
为什么会这样?