保存后保持xml文件的十六进制字符不变?

时间:2017-10-17 15:17:54

标签: c# xml linq-to-xml

使用XDocument对文件进行一些修改后,如何在xml文件中保留十六进制字符? 例如,这是我在任何操作之前的文件

<?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&#x00D7;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">&#x002A;</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>

这是在使用修改后的 Cylian 建议的方法

之后
<!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&#x00D7;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">&#x002A;</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("&", "&amp;").ToString();
case 1:
return input_string.Replace("&amp;", "&").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"[]>。 为什么会这样?

1 个答案:

答案 0 :(得分:1)

用于更清晰的简化代码,简单的控制台应用程序来演示技巧:

Program.cs

using System;
using System.IO;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var basePath = Console.ReadLine();
                string[] filesindirectory = Directory.GetFiles(basePath, "*.xml");
                foreach (string fp in filesindirectory)
                {
                    string file_content = escape_string(File.ReadAllText(fp), 0);
                    XDocument doc = XDocument.Parse(file_content, LoadOptions.PreserveWhitespace);
                    XElement test = new XElement("path", fp.ToString());
                    doc.Root.Add(test);
                    File.WriteAllText(Path.ChangeExtension(fp, "out"), escape_string(doc.ToString(), 1).ToString());
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            Console.ReadKey();
            return;
        }
        private static string escape_string (string input_string, int option){
            switch (option)
            {
                case 0:
                    return input_string.Replace("&", "&amp;").ToString();
                case 1:
                    return input_string.Replace("&amp;", "&").ToString();
                default:
                    return null;

            }
        }
    }
}

test.xml

<test>
<title-group>
<article-title>The coreceptor mutation CCR5&#x0394;32 influences the dynamics of HIV epidemics and is selected for by HIV</article-title>
</title-group>
<contrib-group>
<contrib contrib-type="author">
<name>
<surname>Sullivan</surname>
<given-names>Amy D.</given-names>
</name>
<xref ref-type="author-notes" rid="FN150">&#x002A;</xref>
</contrib>
<contrib contrib-type="author">
<name>
<surname>Wigginton</surname>
<given-names>Janis</given-names>
</name>
</contrib>
<contrib contrib-type="author">
<name>
<surname>Kirschner</surname>
<given-names>Denise</given-names>
</name>
<xref ref-type="author-notes" rid="FN151">&#x2020;</xref>
</contrib>
</contrib-group>
</test>

test.out

<test>
<title-group>
<article-title>The coreceptor mutation CCR5&#x0394;32 influences the dynamics of HIV epidemics and is selected for by HIV</article-title>
</title-group>
<contrib-group>
<contrib contrib-type="author">
<name>
<surname>Sullivan</surname>
<given-names>Amy D.</given-names>
</name>
<xref ref-type="author-notes" rid="FN150">&#x002A;</xref>
</contrib>
<contrib contrib-type="author">
<name>
<surname>Wigginton</surname>
<given-names>Janis</given-names>
</name>
</contrib>
<contrib contrib-type="author">
<name>
<surname>Kirschner</surname>
<given-names>Denise</given-names>
</name>
<xref ref-type="author-notes" rid="FN151">&#x2020;</xref>
</contrib>
</contrib-group>
<path>C:\Users\Cylian\Documents\oo0oo\test.xml</path></test>

希望这可以提供帮助。 (同样,这只是一个快速而肮脏的技巧,已有更好的选择)...... :)