473,834 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
+ 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 " ClientSideOnCli ck="CacheDemo/IntroToCache.as cx"/> has an end tag?

StringBuilder sb = new StringBuilder() ;
if (xmlReader.Node Type == XmlNodeType.Ele ment)
{
while (xmlReader.Move ToNextAttribute ())
{
sb.Append (xmlReader.Name + "=" + xmlReader.Value );

// How can I tell when the end tag "/>" is reached ? ? ? ?
}
}
else if (xmlReader.Node Type == XmlNodeType.End Element)
....

This seems easy enough when the element is following by </MenuItem>. In this case its a matter of looking for xmlReader.NodeT ype == XmlNodeType.End Element. 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 5049
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 misunderstandin g, please feel free to let me
know.

As far as I know, the XmlReader.MoveT oNextAttribute( ) 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.IsEmp tyElement will get you the answer.
Yan

"MLibby" <ml****@nospam. nospam> wrote in message
news:B1******** *************** ***********@mic rosoft.com...
Yes, I understand that when MoveToNextAttri bute() 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 "
ClientSideOnCli ck="CacheDemo/IntroToCache.as cx"/> is converted to
<MenuItem Label="Overview " ClientSideOnCli ck="CacheDemo/IntroToCache.as cx"
> without the ending "/>".

..

private string WriteXml(XmlTex tReader xmlReader)
{
StringBuilder sb = new StringBuilder() ;
sb.Append("<spa n class=xmlDoc>") ;

while (xmlReader.Read ())
{
if (xmlReader.Node Type == XmlNodeType.Ele ment)
{
sb.Append(inden t(xmlReader.Dep th*2));
sb.Append(LT + "<span class=xmlTags>" + xmlReader.Name + "</span>&nbsp");
string elementName = xmlReader.Name;

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

sb.Append("<spa n class=xmlProper ties>" + xmlReader.Name +
"=</span><span class=xmlString s>\"" + xmlReader.Value +
"\"</span>&nbsp");
}

if (xmlReader.IsEm ptyElement )
{
sb.Append(GTE + "<br>");
}
else
{
sb.Append(GT + "<br>");
}
}
else if (xmlReader.Node Type == XmlNodeType.End Element)
{
sb.Append(inden t (xmlReader.Dept h*2));
sb.Append(LTE + "<span class=xmlTags>" + xmlReader.Name + "</span>" + GT +
"<br>");
}
else if (xmlReader.Node Type == XmlNodeType.Tex t)
{
if (xmlReader.Valu e.Length != 0)
{
sb.Append(inden t(xmlReader.Dep th*2));
sb.Append("<spa n class=xmlString s>" + 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(objec t sender, System.EventArg s e)
{
string file = Request.Physica lApplicationPat h + "Include\\" +
Request.Params["File"].ToString();
XmlTextReader xmlReader = new XmlTextReader(f ile);
string str = WriteXml(xmlRea der);
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 misunderstandin g, please feel free to let me
know.

As far as I know, the XmlReader.MoveT oNextAttribute( ) 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.NodeT ype 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.IsEmp tyElement will get you the answer.
Yan

"MLibby" <ml****@nospam. nospam> wrote in message
news:B1******** *************** ***********@mic rosoft.com...
Yes, I understand that when MoveToNextAttri bute() 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 "
ClientSideOnCli ck="CacheDemo/IntroToCache.as cx"/> is converted to
<MenuItem Label="Overview " ClientSideOnCli ck="CacheDemo/IntroToCache.as cx"
> without the ending "/>".

..

private string WriteXml(XmlTex tReader xmlReader)
{
StringBuilder sb = new StringBuilder() ;
sb.Append("<spa n class=xmlDoc>") ;

while (xmlReader.Read ())
{
if (xmlReader.Node Type == XmlNodeType.Ele ment)
{
sb.Append(inden t(xmlReader.Dep th*2));
sb.Append(LT + "<span class=xmlTags>" + xmlReader.Name + "</span> ");
string elementName = xmlReader.Name;

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

sb.Append("<spa n class=xmlProper ties>" + xmlReader.Name +
"=</span><span class=xmlString s>\"" + xmlReader.Value +
"\"</span> ");
}

if (xmlReader.IsEm ptyElement )
{
sb.Append(GTE + "<br>");
}
else
{
sb.Append(GT + "<br>");
}
}
else if (xmlReader.Node Type == XmlNodeType.End Element)
{
sb.Append(inden t (xmlReader.Dept h*2));
sb.Append(LTE + "<span class=xmlTags>" + xmlReader.Name + "</span>" + GT +
"<br>");
}
else if (xmlReader.Node Type == XmlNodeType.Tex t)
{
if (xmlReader.Valu e.Length != 0)
{
sb.Append(inden t(xmlReader.Dep th*2));
sb.Append("<spa n class=xmlString s>" + 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(objec t sender, System.EventArg s e)
{
string file = Request.Physica lApplicationPat h + "Include\\" +
Request.Params["File"].ToString();
XmlTextReader xmlReader = new XmlTextReader(f ile);
string str = WriteXml(xmlRea der);
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 misunderstandin g, please feel free to let me
know.

As far as I know, the XmlReader.MoveT oNextAttribute( ) 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******** *************** ***********@mic rosoft.com...
I just tried that. When iterating through the attributes the xmlReader.NodeT ype
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******** *************** ***********@mic rosoft.com...
I just tried that. When iterating through the attributes the xmlReader.NodeT ype
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
3387
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> <element> 3rd thing </element> </root> would it be possible using xslt only to reverse it into:
4
2398
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 generate any of the specific instances. sample schema one: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="base">
5
3130
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 the defined contents of the TABLE element. The TR element is not defined as an "immediate" or "direct" contained element of TABLE. Given that
6
13382
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... html file (generated using .NET 2.0 beta2): <form method="post" action="Test2.aspx" id="form1">
1
1765
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 in mind). I created a schema from this document. It works fine. Except for some unknown reason, in a small part of the XML document, this program switches the order around, and of course the validator I built then fails. Its always the same two...
4
2924
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 child element with an attribute set at a given value; maybe an example is more self-explaining... Given the following input XML document:
3
11697
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 BEFORE: strCurrency = oSOAP.eServeSearch(CSTR(Request.Form("T1"))) until the WSDL file changed to have more <element name> T1 is just a field that accepts a STRING
8
1950
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 version="1.0" encoding="UTF-8"?> <root> <element>Content</element> <root><element>Content</element></root> <element>Content</element>
2
18157
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 xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Receivers" > <xs:complexType> <xs:choice maxOccurs="unbounded"> <xs:element ref="MulticastReceiver"/>
0
1324
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" xmlns="http://www.tenforce.com/schema/item.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ts="http://www.tenforce.com/rest/timestamp"
0
9796
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9643
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10544
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7755
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5624
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3079
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.