Is it possible to get line numbers/positions from a System.Xml.XmlNode when working with a System.Xml.XmlDocument?
Asked Answered
B

2

8

The title says it all...

Is it possible to get line numbers/positions from a System.Xml.XmlNode when working with a System.Xml.XmlDocument?

I want the information so that I can inform the user of where they need to look in their Xml file for specific pieces of information. I've seen similar questions relating to an XmlReader on SO but nothing that actually answers my question.

I guess I could search the xml file for the OuterXml of the node that I'm interested in, but that seems hacky, and what if that information appears in the file more than once? There must be a better way?

Update: I'm loading my xml file using:

xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load(filename);
Boaten answered 1/2, 2014 at 21:6 Comment(1)
Possible duplicate of C#: Line information when parsing XML with XmlDocumentStephanstephana
A
1

I'm not sure if this is going to help someone, but I had similar objective to the OP. In my particular case, I have an XmlDocument and I needed to have the line number of all the Textbox nodes. Since I was getting the same Exception MadSkunk described, I figured that the only was to create an XPathDocument instance and do the casting.

Richard Schneider's answer helped me to visualise that. So my current method looks like this:

public Dictionary<int, string> GetLineNumber()
{
    Dictionary<int, string> attrByLineNumber = new Dictionary<int, string>();
    MemoryStream xmlStream = new MemoryStream();
    _xmlDoc.Save(xmlStream);
    xmlStream.Position = 0;
    XPathDocument pathDocument = new XPathDocument(xmlStream);
    foreach (XPathNavigator element in pathDocument.CreateNavigator().Select("//*"))
    {
        if (element.Name.Equals("Textbox"))
        {
            attrByLineNumber.Add(((IXmlLineInfo)element).LineNumber, element.GetAttribute("Name", ""));
        }
    }
    return attrByLineNumber;
}
Algebraic answered 14/11, 2016 at 15:1 Comment(0)
A
-1

You can do this using the IXmlLineInfo interface.

I know that with XDocument and XPathDocument you can just cast a node to IXmlLineInfo. I think its also possible with XmlDocument.

Alvie answered 1/2, 2014 at 21:19 Comment(7)
Nope, can't use it with an XmlDocumentBoaten
IXmlLineInfo only helps if the information was set by the underlying XmlReader.Enervate
As John suggested, you need your XmlReader to set line info. Could you post how you reading the XmlDocument.Alvie
I've updated my question: I'm not using an XmlReader.Boaten
Load is sugar for using a XmlReader. Its equivalent to Load(new XmlTextReader(filename). So your in-memory xml document should have the info. Just cast the element/node to IXmlLineInfo.Alvie
Thanks, but I've already tried that. I get Unable to cast object of type 'System.Xml.XmlElement' to type 'System.Xml.IXmlLineInfo'...Boaten
@MadSkunk, did you ever passed this issue? If so, how? I'm exactly at this point of your comment.Algebraic

© 2022 - 2024 — McMap. All rights reserved.