Connecting Tech Pros Worldwide Help | Site Map

Position of XmlNode in XML-File

Christof Nordiek
Guest
 
Posts: n/a
#1: May 3 '07
When I load a XML-Document from a file, and then get an XmlNode from it, is
there any way to get the its original position in the file (line,
character)?

I want to report it, when there is something odd about that node, so that a
user can find the position in the file. Similar to the errormessage from
XmlDocument.Load, if the file isn't wellformed XML.

If that's not possible, would it be possible when examining the XML with
other technics?

thanks
Christof


Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#2: May 3 '07

re: Position of XmlNode in XML-File


Christof,

Using XmlNode, no, there isn't. You would have to parse out the file
yourself, or find the node on your own in the file after you looked at the
corresponding XmlNode.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

"Christof Nordiek" <cn@nospam.dewrote in message
news:%23UgCCEYjHHA.1900@TK2MSFTNGP04.phx.gbl...
Quote:
When I load a XML-Document from a file, and then get an XmlNode from it,
is there any way to get the its original position in the file (line,
character)?
>
I want to report it, when there is something odd about that node, so that
a user can find the position in the file. Similar to the errormessage from
XmlDocument.Load, if the file isn't wellformed XML.
>
If that's not possible, would it be possible when examining the XML with
other technics?
>
thanks
Christof
>

Martin Honnen
Guest
 
Posts: n/a
#3: May 3 '07

re: Position of XmlNode in XML-File


Christof Nordiek wrote:
Quote:
When I load a XML-Document from a file, and then get an XmlNode from it, is
there any way to get the its original position in the file (line,
character)?
Check whether <http://msdn2.microsoft.com/en-gb/library/e3x60fe9.aspx>
helps you implement that.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
John
Guest
 
Posts: n/a
#4: May 3 '07

re: Position of XmlNode in XML-File


On May 3, 8:47 am, "Christof Nordiek" <c...@nospam.dewrote:
Quote:
When I load a XML-Document from a file, and then get an XmlNode from it, is
there any way to get the its original position in the file (line,
character)?
>
I want to report it, when there is something odd about that node, so that a
user can find the position in the file. Similar to the errormessage from
XmlDocument.Load, if the file isn't wellformed XML.
>
If that's not possible, would it be possible when examining the XML with
other technics?
>
thanks
Christof
Even if it were a trivial process to extract the position of the node
within the text you'd have to deal with environments "pretty printing"
the XML. They all seem to do it differently and you can't garauntee
that any one will or will not insert spaces over tabs and place
carriage returns exactly where you believe they should be. There is
no (and there should be no) relationship between the file and the DOM.

One way you may consider doing it is to produce an xpath based on the
node in error and the user can enter that into their specific viewer
to locate it. Unless of course they use a flat text reader. But
applications like Altova XmlSpy ($$) and Doxygen (free as in beer)
will accept an xpath and reliably navigate the document for you.

public string GetXPath(XmlNode child, XmlNode parent)
{
// this will hold our xpath
StringBuilder path = new StringBuilder();
// climb the nodes from the incoming node 'n' upto the root
for (XmlNode p = child; p != parent && p.ParentNode != null; p =
p.ParentNode)
{
// get the element index by counting the previous siblings
int sibCount = 1;
for (XmlNode pn = p.PreviousSibling;
pn != null;
pn = pn.PreviousSibling, ++sibCount) ;
// prepend it to our xpath
path.Insert(0, "/node()[" + sibCount + "]");
}
//Prepend this for the document root.
path.Insert(0, "/node()[1]");
// and return the completed xpath to the incoming node
return path.ToString();
}

You can use the above method to generate an xpath for a specific node
upto a specific parent. Another version of this would automatically
default the parent to the root of the document if that's all you
needed.

Hope this helps,
John

=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
 
Posts: n/a
#5: May 3 '07

re: Position of XmlNode in XML-File


Christof,
the kind of "line and character" etc. reporting that you describe is
available to you when you validate the XmlDocument against a schema, using
XmlValidatingReader or any of several other methods. If you do not have a
schema, you can create one.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net




"Christof Nordiek" wrote:
Quote:
When I load a XML-Document from a file, and then get an XmlNode from it, is
there any way to get the its original position in the file (line,
character)?
>
I want to report it, when there is something odd about that node, so that a
user can find the position in the file. Similar to the errormessage from
XmlDocument.Load, if the file isn't wellformed XML.
>
If that's not possible, would it be possible when examining the XML with
other technics?
>
thanks
Christof
>
>
>
Christof Nordiek
Guest
 
Posts: n/a
#6: May 7 '07

re: Position of XmlNode in XML-File


"Peter Bromberg [C# MVP]" <pbromberg@yahoo.yabbadabbadoo.comschrieb im
Newsbeitrag news:FF297C5E-D0C2-4E7D-8299-ED8710079433@microsoft.com...
Quote:
Christof,
the kind of "line and character" etc. reporting that you describe is
available to you when you validate the XmlDocument against a schema, using
XmlValidatingReader or any of several other methods. If you do not have a
schema, you can create one.
Peter
>
Hi Peter,

thanks for responding

Surely I could validate some rules by using a schema. But besides the minor
obstacle, that I would have to create a schemadefinition file, there are
some rules, I can't define by a schema file.
E.g. there can be a reference to a class in an assembly, and that class will
have to implement a certain interface. I think I can't define such a rule
with a schema. But still I want to point to the part of the file, wich
referes to that wrong type.

Christof


Closed Thread