C#无法在asp.net中读取XML文件

时间:2012-04-24 17:28:45

标签: c# asp.net xml visual-studio

我正在尝试使用以下代码读取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 个答案:

答案 0 :(得分:4)

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

波形路径(officially, "Web Application root operator"仅适用于服务器控件和其他支持ASP.NET的实用程序 - 不适用于需要路径的任何位置。

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

new XmlTextReader(Server.MapPath("~/bin/config.xml"))

答案 1 :(得分:1)

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

XmlTextReader reader = new XmlTextReader(Server.MapPath("~/bin/config.xml"));

答案 2 :(得分:0)

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

XmlDocument xDoc = new XmlDocument();
        xDoc.LoadXml("~path/mydoc.xml");
        foreach (XmlNode xNode in xDoc.ChildNodes)
        {
            //Do w.e
        }

答案 3 :(得分: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);
    }
}