1

我正在尝试使用如下代码读取 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将不胜感激非常感谢!

4

4 回答 4

4
new XmlTextReader("~/bin/config.xml")

波浪号路径(正式地,“Web 应用程序根运算符”仅适用于服务器控件和其他 ASP.NET 感知实用程序 - 不需要路径的任何地方。

您可以使用Server.MapPath来获取文件的物理位置。

new XmlTextReader(Server.MapPath("~/bin/config.xml"))
于 2012-04-24T17:42:57.937 回答
1

您可能需要将相对 ASP.NET 路径转换为物理路径。请参阅HttpServerUtility.MapPath

XmlTextReader reader = new XmlTextReader(Server.MapPath("~/bin/config.xml"));
于 2012-04-24T17:43:42.297 回答
0

Linq2Xml 更容易使用

XDocument xDoc = XDocument.Load(....);
var dict = xDoc.Element("files")
    .Descendants()
    .ToDictionary(k=>k.Name,v=>v.Attributes().ToDictionary(ak=>ak.Name,av=>av.Value));

foreach (var item in dict)
{
    Console.WriteLine(item.Key+":");
    foreach (var attr in item.Value)
    {
        Console.WriteLine("\t"+attr.Key + "="+ attr.Value);
    }
}
于 2012-04-24T17:57:17.250 回答
0

我从未使用过 XmlTextReader,而是使用它似乎更容易使用。

XmlDocument xDoc = new XmlDocument();
        xDoc.LoadXml("~path/mydoc.xml");
        foreach (XmlNode xNode in xDoc.ChildNodes)
        {
            //Do w.e
        }
于 2012-04-24T17:36:38.260 回答