472,125 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,125 software developers and data experts.

Element End Tag

How can you tell when you've reached an elements end tag? For example, how can the following code tell that <MenuItem Label="Overview" ClientSideOnClick="CacheDemo/IntroToCache.ascx"/> has an end tag?

StringBuilder sb = new StringBuilder();
if (xmlReader.NodeType == XmlNodeType.Element)
{
while (xmlReader.MoveToNextAttribute())
{
sb.Append (xmlReader.Name + "=" + xmlReader.Value);

// How can I tell when the end tag "/>" is reached ? ? ? ?
}
}
else if (xmlReader.NodeType == XmlNodeType.EndElement)
....

This seems easy enough when the element is following by </MenuItem>. In this case its a matter of looking for xmlReader.NodeType == XmlNodeType.EndElement. Not sure how to do this when there is an end tag is inside of the element itself.

Thanks for any help,
Mike
--
mcp, mcse, mcsd, mcad.net, mcsd.net
Nov 12 '05 #1
5 4946
Hi Mike,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to know how to check for end
tag in Xml. If there is any misunderstanding, please feel free to let me
know.

As far as I know, the XmlReader.MoveToNextAttribute() method returns true
when it has a next attribute, and will return false when no next attribute
is available. So if it returns false, it means that the end tag is reached
and the it will jump out the while block in your program. So I don't think
we need to check for the end tag explicitly. Just add what you want to do
after the while block.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #2
xmlReader.IsEmptyElement will get you the answer.
Yan

"MLibby" <ml****@nospam.nospam> wrote in message
news:B1**********************************@microsof t.com...
Yes, I understand that when MoveToNextAttribute() is complete the loop
exits. The problem is that I don't know if the element has ended "/>" or
if the element ends further elsewhere "</end>".

The following is trying to reformat an xml document for browser display
using css styles. It will not end elements with embeded end tags "/>".
For example <MenuItem Label="Overview"
ClientSideOnClick="CacheDemo/IntroToCache.ascx"/> is converted to
<MenuItem Label="Overview" ClientSideOnClick="CacheDemo/IntroToCache.ascx"
> without the ending "/>".

..

private string WriteXml(XmlTextReader xmlReader)
{
StringBuilder sb = new StringBuilder();
sb.Append("<span class=xmlDoc>");

while (xmlReader.Read())
{
if (xmlReader.NodeType == XmlNodeType.Element)
{
sb.Append(indent(xmlReader.Depth*2));
sb.Append(LT + "<span class=xmlTags>" + xmlReader.Name + "</span>&nbsp");
string elementName = xmlReader.Name;

while (xmlReader.MoveToNextAttribute())
{
if (elementName == "MenuData")
sb.Append("<br>&nbsp");

sb.Append("<span class=xmlProperties>" + xmlReader.Name +
"=</span><span class=xmlStrings>\"" + xmlReader.Value +
"\"</span>&nbsp");
}

if (xmlReader.IsEmptyElement )
{
sb.Append(GTE + "<br>");
}
else
{
sb.Append(GT + "<br>");
}
}
else if (xmlReader.NodeType == XmlNodeType.EndElement)
{
sb.Append(indent (xmlReader.Depth*2));
sb.Append(LTE + "<span class=xmlTags>" + xmlReader.Name + "</span>" + GT +
"<br>");
}
else if (xmlReader.NodeType == XmlNodeType.Text)
{
if (xmlReader.Value.Length != 0)
{
sb.Append(indent(xmlReader.Depth*2));
sb.Append("<span class=xmlStrings>" + xmlReader.Value + "</span><br>");
}
}
}
sb.Append("</span>");
return sb.ToString();
}

private string GT {get {return "<span class=xmlGTLT>></span>";}}
private string GTE{get {return "<span class=xmlGTLT>/></span>";}}
private string LT {get {return "<span class=xmlGTLT><</span>";}}
private string LTE {get {return "<span class=xmlGTLT></</span>";}}

private string indent (int number)
{
StringBuilder sb = new StringBuilder();
for (int i=0; i < number; i++)
{
sb.Append (" ");
}
return sb.ToString();
}

private void Page_Load(object sender, System.EventArgs e)
{
string file = Request.PhysicalApplicationPath + "Include\\" +
Request.Params["File"].ToString();
XmlTextReader xmlReader = new XmlTextReader(file);
string str = WriteXml(xmlReader);
Response.Write(str);
}

.css file
.xmlTags
{
color: maroon;
}

.xmlProperties
{
color: red;
}

.xmlStrings
{
color: blue;
}

.xmlGTLT
{
color: blue;
}

.xmlDoc
{
font-size: 10px;
line-height: 12px;
font-family: 'Microsoft Sans Serif' , Verdana;
}

Please let me know if this makes more sense and how to determine if an
element has an "/>" tag so I can correctly format it for output.

Kind regards,
Mike
--
mcp, mcse, mcsd, mcad.net, mcsd.net
"Kevin Yu [MSFT]" wrote:
Hi Mike,

First of all, I would like to confirm my understanding of your issue.
From
your description, I understand that you need to know how to check for end
tag in Xml. If there is any misunderstanding, please feel free to let me
know.

As far as I know, the XmlReader.MoveToNextAttribute() method returns true
when it has a next attribute, and will return false when no next
attribute
is available. So if it returns false, it means that the end tag is
reached
and the it will jump out the while block in your program. So I don't
think
we need to check for the end tag explicitly. Just add what you want to do
after the while block.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #3
I just tried that. When iterating through the attributes the xmlReader.NodeType always equals Attribute. Upon exiting the loop the NodeType is still Attribute not EndElement. However, if there is an element is specifially an </endtag> then the NodeType is set to EndElement.

Please let me know how to determine if an element containing attributes has an "/>" tag.

Thanks,
Mike
--
mcp, mcse, mcsd, mcad.net, mcsd.net
"Yan Leshinsky" wrote:
xmlReader.IsEmptyElement will get you the answer.
Yan

"MLibby" <ml****@nospam.nospam> wrote in message
news:B1**********************************@microsof t.com...
Yes, I understand that when MoveToNextAttribute() is complete the loop
exits. The problem is that I don't know if the element has ended "/>" or
if the element ends further elsewhere "</end>".

The following is trying to reformat an xml document for browser display
using css styles. It will not end elements with embeded end tags "/>".
For example <MenuItem Label="Overview"
ClientSideOnClick="CacheDemo/IntroToCache.ascx"/> is converted to
<MenuItem Label="Overview" ClientSideOnClick="CacheDemo/IntroToCache.ascx"
> without the ending "/>".

..

private string WriteXml(XmlTextReader xmlReader)
{
StringBuilder sb = new StringBuilder();
sb.Append("<span class=xmlDoc>");

while (xmlReader.Read())
{
if (xmlReader.NodeType == XmlNodeType.Element)
{
sb.Append(indent(xmlReader.Depth*2));
sb.Append(LT + "<span class=xmlTags>" + xmlReader.Name + "</span> ");
string elementName = xmlReader.Name;

while (xmlReader.MoveToNextAttribute())
{
if (elementName == "MenuData")
sb.Append("<br> ");

sb.Append("<span class=xmlProperties>" + xmlReader.Name +
"=</span><span class=xmlStrings>\"" + xmlReader.Value +
"\"</span> ");
}

if (xmlReader.IsEmptyElement )
{
sb.Append(GTE + "<br>");
}
else
{
sb.Append(GT + "<br>");
}
}
else if (xmlReader.NodeType == XmlNodeType.EndElement)
{
sb.Append(indent (xmlReader.Depth*2));
sb.Append(LTE + "<span class=xmlTags>" + xmlReader.Name + "</span>" + GT +
"<br>");
}
else if (xmlReader.NodeType == XmlNodeType.Text)
{
if (xmlReader.Value.Length != 0)
{
sb.Append(indent(xmlReader.Depth*2));
sb.Append("<span class=xmlStrings>" + xmlReader.Value + "</span><br>");
}
}
}
sb.Append("</span>");
return sb.ToString();
}

private string GT {get {return "<span class=xmlGTLT>></span>";}}
private string GTE{get {return "<span class=xmlGTLT>/></span>";}}
private string LT {get {return "<span class=xmlGTLT><</span>";}}
private string LTE {get {return "<span class=xmlGTLT></</span>";}}

private string indent (int number)
{
StringBuilder sb = new StringBuilder();
for (int i=0; i < number; i++)
{
sb.Append (" ");
}
return sb.ToString();
}

private void Page_Load(object sender, System.EventArgs e)
{
string file = Request.PhysicalApplicationPath + "Include\\" +
Request.Params["File"].ToString();
XmlTextReader xmlReader = new XmlTextReader(file);
string str = WriteXml(xmlReader);
Response.Write(str);
}

.css file
.xmlTags
{
color: maroon;
}

.xmlProperties
{
color: red;
}

.xmlStrings
{
color: blue;
}

.xmlGTLT
{
color: blue;
}

.xmlDoc
{
font-size: 10px;
line-height: 12px;
font-family: 'Microsoft Sans Serif' , Verdana;
}

Please let me know if this makes more sense and how to determine if an
element has an "/>" tag so I can correctly format it for output.

Kind regards,
Mike
--
mcp, mcse, mcsd, mcad.net, mcsd.net
"Kevin Yu [MSFT]" wrote:
Hi Mike,

First of all, I would like to confirm my understanding of your issue.
From
your description, I understand that you need to know how to check for end
tag in Xml. If there is any misunderstanding, please feel free to let me
know.

As far as I know, the XmlReader.MoveToNextAttribute() method returns true
when it has a next attribute, and will return false when no next
attribute
is available. So if it returns false, it means that the end tag is
reached
and the it will jump out the while block in your program. So I don't
think
we need to check for the end tag explicitly. Just add what you want to do
after the while block.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."


Nov 12 '05 #4
"MLibby" <ml****@nospam.nospam> wrote in message news:B4**********************************@microsof t.com...
I just tried that. When iterating through the attributes the xmlReader.NodeType
always equals Attribute. Upon exiting the loop the NodeType is still Attribute
not EndElement.


When you are finished in the while loop with looping through all of the Attribute
nodes, you must call MoveToElement( ) to return to the element containing the
attributes. After you've positioned yourself back on the XmlElement node that
contains these attributes, you can query IsEmptyElement as Yan suggested.
Derek Harmon
Nov 12 '05 #5
Thank you Derek! That was the piece I was missing.

Mike
--
mcp, mcse, mcsd, mcad.net, mcsd.net
"Derek Harmon" wrote:
"MLibby" <ml****@nospam.nospam> wrote in message news:B4**********************************@microsof t.com...
I just tried that. When iterating through the attributes the xmlReader.NodeType
always equals Attribute. Upon exiting the loop the NodeType is still Attribute
not EndElement.


When you are finished in the while loop with looping through all of the Attribute
nodes, you must call MoveToElement( ) to return to the element containing the
attributes. After you've positioned yourself back on the XmlElement node that
contains these attributes, you can query IsEmptyElement as Yan suggested.
Derek Harmon

Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Gordon Dickens | last post: by
6 posts views Thread by Luke Dalessandro | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.