2012-04-24 20 views
1

我试图用下面的代码读取一个xml文件。C#无法读取asp.net中的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> 

但一些如何Im无法读取它。这里是我用来尝试读取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

并将异常..... – scottheckel 2012-04-24 17:31:11

+2

你是如何 “无法读取它”? – Tejs 2012-04-24 17:32:47

+0

当我尝试获取infolder的路径时,它给了我一个ArgumentNullException。 – 2012-04-25 01:19:14

回答

0

我从来没有使用XmlTextReader,与此相反,它似乎很容易处理。

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

的波浪路径(officially, "Web Application root operator"仅适用于服务器控件和其他ASP.NET的实用程序 - 在任何地方,需要一个路径

您可以使用Server.MapPath得到一个物理位置的。文件中。

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

该文件是一个.CS文件,所以我不认为我能在这种情况下使用Server.MapPath – 2012-04-25 01:59:23

+0

假设您在ASP.NET中运行.cs,请使用HttpContext.Current.Server.MapPath – 2012-04-25 13:44:57

+0

如果.cs是一个控制台应用程序,并试图让它读取xml文件,该怎么办?然后使用ASP.NET调用读取xml文件的cs文件 – 2012-04-25 17:41:20

1

你很可能需要相对ASP.NET路径转换为物理路径。见HttpServerUtility.MapPath

XmlTextReader reader = new XmlTextReader(Server.MapPath("~/bin/config.xml")); 
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); 
    } 
}