有没有办法让我的C#XMLManager正常工作?
所以我完全是C#的傻瓜,有什么办法可以让这一切顺利进行吗? 如果有人能解释为什么我的系统不工作,以及为什么另一个版本会工作,这也将是一个很大的帮助有没有办法让我的C#XMLManager正常工作?,c#,xml,C#,Xml,所以我完全是C#的傻瓜,有什么办法可以让这一切顺利进行吗? 如果有人能解释为什么我的系统不工作,以及为什么另一个版本会工作,这也将是一个很大的帮助 using System; using System.Xml; public class XMLManager { private XmlTextReader reader; private XmlDocument document; private XmlNodeList nodeList; public voi
using System;
using System.Xml;
public class XMLManager
{
private XmlTextReader reader;
private XmlDocument document;
private XmlNodeList nodeList;
public void OpenFile(string file)
{
try
{
reader = new XmlTextReader(file);
reader.WhitespaceHandling = WhitespaceHandling.None;
reader.MoveToContent();
document = new XmlDocument();
document.Load(reader);
nodeList = document.SelectNodes(@"Settings/Settings");
}
catch (System.IO.FileNotFoundException)
{
}
}
public void CloseFile()
{
if (reader != null)
{
((IDisposable)reader).Dispose();
reader.Close();
reader = null;
}
document = null;
nodeList = null;
}
public string Get(string attrib)
{
for (int i = 0; i < nodeList.Count; i++)
{
reader.MoveToAttribute(i);
if (reader.Name == attrib)
{
return reader.Value;
}
}
return null;
}
}
使用系统;
使用System.Xml;
公共类XMLManager
{
私有XmlTextReader;
私人文件;
私有XmlNodeList节点列表;
公共void OpenFile(字符串文件)
{
尝试
{
reader=新的XmlTextReader(文件);
reader.WhitespaceHandling=WhitespaceHandling.None;
reader.MoveToContent();
document=新的XmlDocument();
文件加载(读卡器);
nodeList=document.SelectNodes(@“设置/设置”);
}
捕获(System.IO.FileNotFoundException)
{
}
}
公共文件()
{
if(读卡器!=null)
{
((IDisposable)读取器).Dispose();
reader.Close();
reader=null;
}
document=null;
nodeList=null;
}
公共字符串获取(字符串属性)
{
for(int i=0;i
编辑:很抱歉我的格式不好,这是我第一次发布关于堆栈溢出的帖子。总的来说,你做的工作太多了 如果文件中有XML,请使用XmlDocument.load(strFileName)将其直接加载到XML(DOM)对象中 要迭代匹配XPath查询的所有节点,请参阅如何运行它们
try {
string strFileName = HttpContext.Current.Server.MapPath("\\data.xml");
XmlDocument xml = new XmlDocument();
xml.Load( strFileName );
foreach (XmlElement ndRow in xml.SelectNodes("//row")) {
string strTemp = ndRow.GetAttribute("foo");
}
} catch (Exception ex) {
Response.Write(ex.Message);
}
你在这里犯了很多错误 首先,将xml内容读入xml文档不需要读取器 其次,在尝试获取属性时,您正在尝试使用读取器继续获取属性,而读取器显然不具有所选节点的上下文 这是您更新的XmlManager,但我必须注意,还有一个逻辑错误,即当调用GetAttribute时,您正在搜索所有设置/设置节点,如果在其中任何节点中找到属性,则返回它。如果xml文件只包含一个设置/设置节点,
SelectSingleNode
更好。我采用以下格式:
<Settings>
<Settings attr1="attr1val" attr2="attr2val" />
</Settings>
注意:我还删除了CloseFile方法,因为它不再是必需的
public class XMLManager
{
private XmlDocument document;
private XmlNodeList nodeList;
public void OpenFile(string file)
{
document = new XmlDocument();
document.Load(file);
nodeList = document.SelectNodes(@"Settings/Settings");
}
public string Get(string attrib)
{
for (int i = 0; i < nodeList.Count; i++)
{
if (nodeList[i].Attributes[attrib] != null)
{
return nodeList[i].Attributes[attrib].Value;
}
}
return null;
}
}
公共类XMLManager
{
私人文件;
私有XmlNodeList节点列表;
公共void OpenFile(字符串文件)
{
document=新的XmlDocument();
文件加载(文件);
nodeList=document.SelectNodes(@“设置/设置”);
}
公共字符串获取(字符串属性)
{
for(int i=0;i
有人怎么告诉你为什么你的系统不工作。。还有什么是不工作的意思???
您还应该使用调试器并逐步完成代码。。如果它在你的机器上,为什么你不能调试它。。?此外这不是代码工厂/测试站点。。再一次,如果你知道如何正确使用调试器,它就是你的朋友;发生了什么你没有预料到的事情,或者发生了什么你没有预料到的事情?你遇到了什么问题?你说我的系统不工作,但没有描述问题。看一看。很好,您可以展示不起作用的代码——现在,告诉我们它做错了什么,包括一个导致问题的XML示例。问题是什么?什么不起作用?我可以看出你做错了一件事——你正在捕获并忽略FileNotFoundException
。如果您的文件丢失,您的代码将在没有通知的情况下自动失败。非常感谢您的精彩解释和修复的代码,帮助我更好地理解“System.Xml”。另外,SelectSingleNode
正是我想要的。我只是想确保它能正常工作,效果很好。谢谢贴前测试:)谢谢