【问题标题】:Error - Not Supported Exception was unhandled by user code错误 - 用户代码未处理不支持的异常
【发布时间】:2016-02-05 09:48:47
【问题描述】:

我有一个 XML 文件,我想根据从下拉列表中选择的值动态更新它。所以这是我尝试的逻辑,

读取和更新 XML:-

private void ReadandUpdateXML(string CMEURL)
        {
            XmlDocument doc = new XmlDocument();
            string inputxmlPath = ConfigurationManager.AppSettings["InputDataFileName"];
            doc.Load(inputxmlPath);
            var ParentNode = doc.SelectSingleNode("//TestData");
            if (ParentNode.ChildNodes.Count > 0)
            {
                foreach (XmlNode child in ParentNode)
                {
                    for (int i = 0; i < child.ChildNodes.Count; i++)
                    {
                        child.ChildNodes[i].InnerText = CMEURL;
                    }
                }
                doc.Save(inputxmlPath);
            }
        }

更新 XML 值后我有一个要求,我需要阅读该要求

读取 XML:-

private static List<TestData> GetInputData(string NodeName)
        {
            string InputFilePath = AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["InputDataFileName"];// "InputData.xml";
            Stream inputStream = File.Open(InputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);// getting exception here
            XElement xelement = XElement.Load(inputStream);
            var name = from nm in xelement.Elements(NodeName).Descendants()
                       select new
                       {
                           Input = System.Text.RegularExpressions.Regex.Replace(nm.Value, @"\t|\n|\r", "").Trim(),
                           Result = (nm.Attribute("Result") == null ? string.Empty : System.Text.RegularExpressions.Regex.Replace(nm.Attribute("Result").Value, @"\t|\n|\r", "").Trim())
                       };

            List<TestData> tdList = new List<TestData>();
            foreach (var item in name.ToList())
            {
                if (!string.IsNullOrEmpty(item.Input))
                {
                    TestData td = new TestData();
                    td.Input = item.Input;
                    td.Result = item.Result;
                    tdList.Add(td);
                }
            }

            return tdList;
        }

Stream inputStream = File.Open(InputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); // Getting exception here

异常:- 不支持 异常未被用户代码处理。不支持给定路径的格式。

读取 XML 逻辑之前工作正常。但是在更新 xml 之后它会抛出一个错误。 不知道我做错了什么.. 谁能帮帮我?

【问题讨论】:

  • 请出示minimal reproducible example 来说明问题,并包括完整的堆栈跟踪 - 目前我们不知道代码的哪一部分引发了异常。

标签: xml c#-4.0 xpath


【解决方案1】:

伙计们!我为我的问题找到了解决方案。

在 web.config 中:- 我得到如下文件路径

<add key="InputDataFileName" value="C:\Users\vishnu\Desktop\PQC project\source\Tridion.AutomationDashboard\InputData.xml" />

我是这样读的

string inputxmlPath = ConfigurationManager.AppSettings["InputDataFileName"];

在阅读 XML 代码中,我得到了该文件路径的基本目录,如下所示

string InputFilePath = AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["InputDataFileName"];

所以 InputFilePath 被附加了两次。这就是它抛出错误的原因。

我的错,我没有告诉任何关于 web.config 文件的信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 2017-12-12
    相关资源
    最近更新 更多