Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在xml中找到特定节点的行号?_C#_Xml_Numbers_Line - Fatal编程技术网

C# 如何在xml中找到特定节点的行号?

C# 如何在xml中找到特定节点的行号?,c#,xml,numbers,line,C#,Xml,Numbers,Line,我试图访问发生错误的xml文档中特定节点的行号(例如,命名错误的属性)。以下是我迄今为止确定错误行号的类的代码: public class LineNumberFind { private XmlNamespaceManager nsmgr; private XmlParserContext context; public XmlTextReader reader; public LineNumberFind(XmlDocument doc) {

我试图访问发生错误的xml文档中特定节点的行号(例如,命名错误的属性)。以下是我迄今为止确定错误行号的类的代码:

public class LineNumberFind
{
    private XmlNamespaceManager nsmgr;
    private XmlParserContext context;
    public XmlTextReader reader;

    public LineNumberFind(XmlDocument doc)
    {
        StringWriter sw = new StringWriter();
        XmlTextWriter tx = new XmlTextWriter(sw);
        doc.WriteTo(tx);
        string str = sw.ToString();
        //nsmgr = new XmlNamespaceManager(new NameTable());
        //context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);
        reader = new XmlTextReader(str);
    }

    public int NamingErrorLine(XmlNode node)
    {
        reader.MoveToContent();
        while (reader.Read())
        {
            if (reader.NodeType == XmlNodeType.Element)
            {
                reader.MoveToAttribute(0);
                if (reader.Value.ToString() == node.Attributes["name"].Value.ToString())
                    return reader.LineNumber;
            }
        }
        return 0;
    }
}
以及我尝试使用NamingErrorLine方法的代码片段:

foreach (XmlNode item in doc.SelectNodes("configuration/events/Event"))
        {
            EventEnforce eventNode = new EventEnforce(syntaxError, item);
            if (item.Attributes["name"] != null && item.Attributes["cond"] != null)
            {
                // colorDict returns an int between 0 and 1 with 0 meaning
                // that it is an Action, 1 is a condition, and 2 is Event
                try
                {
                    eventName.Add(item.Attributes["name"].Value);
                    colorDict.Add(item.Attributes["name"].Value, 2);
                    eventDictionary.Add(item.Attributes["cond"].Value, item.Attributes["name"].Value);
                    eventNameToCondDict.Add(item.Attributes["name"].Value, item.Attributes["cond"].Value);
                }
                catch
                {
                    nameingErrors.Add("\tDuplicate entry found. Error at element: <event name=\"" + item.Attributes["name"].Value + "\".../>");
                    MessageBox.Show(lnf.NamingErrorLine(item).ToString());
                    containsSyntaxError = true;
                }
            }

        }
foreach(doc.SelectNodes(“配置/事件/事件”)中的XmlNode项)
{
EventEnforce eventNode=新的EventEnforce(语法错误,项);
if(item.Attributes[“name”]!=null和&item.Attributes[“cond”]!=null)
{
//colorDict返回一个介于0和1之间的整数,其含义为0
//它是一个动作,1是一个条件,2是一个事件
尝试
{
添加(item.Attributes[“name”].Value);
添加(item.Attributes[“name”]。值,2);
添加(item.Attributes[“cond”].Value,item.Attributes[“name”].Value);
eventNameToCondDict.Add(item.Attributes[“name”].Value,item.Attributes[“cond”].Value);
}
抓住
{
nameingErrors.Add(“\t找到重复项。元素处出错:”;
Show(lnf.NamingErrorLine(item.ToString());
containsSyntaxError=true;
}
}
}
现在它告诉我正在使用的字符串(string str=sw.ToString())太长,无法在XmlTextReader(string str)中使用。有没有更好的办法?我已经搜索了一段时间,但没有找到任何其他内容。

XmlTextReader(字符串)构造函数需要的是URI,而不是xml文本(请参阅)

相反,请考虑:

var reader = new XmlTextReader(new StringReader(sw.ToString()));

可以将XML作为字符串加载吗。将其拆分成几行并搜索包含关键字的行