我正在尝试使用如下代码读取 xml 文件。
<?xml version="1.0" encoding="utf-8" ?>
<files>
<pdf_input infolder ="C:\Users\Lambo\Documents\Visual Studio 2010\Projects\test\test\testPdfIn"
outfolder ="C:\Users\Lambo\Documents\Visual Studio 2010\Projects\test\test\testPdfOut"
autonameappend="_new" />
<word_file infolder =" C:\Users\Lambo\Documents\Visual Studio 2010\Projects\test\test\testPdfIn"
outfolder ="C:\Users\Lambo\Documents\Visual Studio 2010\Projects\test\test\testPdfOut" />
<pdf_file fileRequired="true" directory="" autonameappend="pdf" />
<docx_file fileRequired="true" directory="" autonameappend="docx" />
<!-- autonameappend: Such as: (copy) -->
<doc_file fileRequired="true" removePicture="true" removeFormfield="true" directory="" autonameappend="_new" />
</files>
但有些我无法阅读它。这是我用来尝试读取 xml 文件的代码。
public static void readConfig()
{
try
{
// StreamReader sr = new StreamReader("");
XmlTextReader reader = new XmlTextReader("~/bin/config.xml");
reader.MoveToContent();
reader.ReadToDescendant("pdf_input");
pdf_infolder = reader.GetAttribute("infolder");
pdf_outfolder = reader.GetAttribute("outfolder");
pdf_nameAppend = reader.GetAttribute("autonameappend");
MessageBox.Show("two passed");
word_outfolder = reader.GetAttribute("outfolder");
reader.ReadToNextSibling("pdf_file");
pdf_required = Convert.ToBoolean(reader.GetAttribute("fileRequired"));
pdf_newDirectoryV=reader.GetAttribute("directory");
pdf_autoName = reader.GetAttribute("autonameappend");
MessageBox.Show("3 passed");
reader.ReadToNextSibling("docx_file");
docx_required = Convert.ToBoolean(reader.GetAttribute("fileRequired"));
docx_newDirectoryV=reader.GetAttribute("directory");
docx_autoName = reader.GetAttribute("autonameappend");
MessageBox.Show("4 passed");
reader.ReadToNextSibling("doc_file");
doc_required = Convert.ToBoolean(reader.GetAttribute("fileRequired"));
doc_removePic = Convert.ToBoolean(reader.GetAttribute("removePicture"));
doc_removeFF = Convert.ToBoolean(reader.GetAttribute("removeFormfield"));
doc_newDirectoryV=reader.GetAttribute("directory");
doc_autoName = reader.GetAttribute("autonameappend");
reader.Close();
// MessageBox.Show("Success");
// MessageBox.Show("pdf_required is :" + pdf_required + " pdf_newdirectory is :" + pdf_newDirectoryV + "End");
}
catch (Exception)
{
MessageBox.Show("reading config file failed, using default value instead" );
restoreDefault();
}
}
private static void restoreDefault()
{
// wordName = @"C:\Users\user\Documents\Visual Studio 2010\Projects\SecureWord\SecureWord\bin\Debug\Sample3.doc";
pdf_required = true;
pdf_newDirectoryV = "";
pdf_autoName = "";
docx_required = true;
docx_newDirectoryV = "";
docx_autoName = "";
doc_required = true;
doc_removePic = true;
doc_removeFF = true;
doc_newDirectoryV = "";
doc_autoName = "";
}
}
Anyhelp将不胜感激非常感谢!