473,473 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 5030
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Igor | last post by:
Is there any way to resort and xml document using xslt based on element position. For example if I have xml like this: <root> <element> 1st thing </element> <element> 2nd thing </element>...
4
by: Gordon Dickens | last post by:
I have target xml to generate from schema. All of the XML instances have the same global element i.e. <base>. I would like to combine all of the schemas into a single schema where I could...
5
by: Patient Guy | last post by:
In my reading of the Strict and Transitional DTD for HTML 4.0, the table row (TR) elements are contained within table section elements: THEAD, TFOOT, and TBODY. The table section elements are...
6
by: Luke Dalessandro | last post by:
I'm not sure if this is the correct forum for platform specific (Mozilla/Firefox) javascript problems, so just shout and point me to the correct newsgroup if I'm being bad. Here's the deal... ...
1
by: jrmsmo | last post by:
Hi there, I have an interesting problem that maybe you pros can suggest how I solve. I'm working with a third party program that serializes an XML document (it was obviously not designed with schema...
4
by: patrizio.trinchini | last post by:
Hi all, I'm new to XSLT and maybe my problem have a very trivial answer, but I need an expert that point me in the right direction. What I would obtain is to remove all the elements that have a...
3
by: jparulan | last post by:
Hi All, I'm using SOAP3.0. I was able to successfully call a WSDL file and get a value properly. But when the WSDL changed to have a MULTIPLE <element name> it was failing. This code works...
8
by: VK | last post by:
Can be multiple instances of element used as the root element? That's a curly way of asking, but I did not come up with a better sentence, sorry. What I mean is with a document like: <?xml...
2
by: mlb5000 | last post by:
I seem to be having issues validating an XML document using my schema. Both are below: The Schema: <?xml version="1.0" encoding="UTF-8"?> <xs:schema...
0
Airslash
by: Airslash | last post by:
Hello, i've written the following XML Schema to validate my custom XML: <?xml version="1.0" encoding="utf-8"?> <xs:schema id="item" targetNamespace="http://www.tenforce.com/rest/item" ...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.