gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:26:34 26 4
gpt4 key购买 nike

我正在尝试使用以下代码读取 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 = "";

}

任何帮助将不胜感激非常感谢!

最佳答案

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

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

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

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

关于C# 无法在 asp.net 中读取 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10302948/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com