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

Home Posts Topics Members FAQ

XmlTextWriter and suppressing empty elements

Howdy:

I am currently writing a .Net handler to replace an ASP page. The ASP
module used a COM dll to format XML. This dll was pretty robust and
it auto-suppressed empty elements and attributes. A developer in my
group is porting the code into .Net, but I an still believe that there
is a way to do this in the native .Net classes. Is anyone familiar
with a way to do this using XmlTextWriter or another related class?

I found code that will allow me to load the resulting XML into an
XMLDocument and then I could use an XPath expression to remove the
empty elements. This is a potential solution, however the application
I am replacing didn't necessarily format syntatically correct XML. It
creates a bunch of elements with attributes and no value.
i.e. <element1 att1="att1"></element1>
So I need to account for that.

Please let me know what you think and thanks,
Tom
Nov 12 '05 #1
4 3344
T Conti wrote:
I am currently writing a .Net handler to replace an ASP page. The ASP
module used a COM dll to format XML. This dll was pretty robust and
it auto-suppressed empty elements and attributes. A developer in my
group is porting the code into .Net, but I an still believe that there
is a way to do this in the native .Net classes. Is anyone familiar
with a way to do this using XmlTextWriter or another related class?


What do you mean saying "supress"? XmlTextWriter exposes WriteXXX
methods, which allows to write XML. And it's your responsibility to call
them or not.

--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #2
"Oleg Tkachenko [MVP]" <oleg@no_!spam!_please!tkachenko.com> wrote in message news:<uD**************@TK2MSFTNGP11.phx.gbl>...
T Conti wrote:
I am currently writing a .Net handler to replace an ASP page. The ASP
module used a COM dll to format XML. This dll was pretty robust and
it auto-suppressed empty elements and attributes. A developer in my
group is porting the code into .Net, but I an still believe that there
is a way to do this in the native .Net classes. Is anyone familiar
with a way to do this using XmlTextWriter or another related class?


What do you mean saying "supress"? XmlTextWriter exposes WriteXXX
methods, which allows to write XML. And it's your responsibility to call
them or not.


Thanks for your posting...

The situation I am running into is that I write several nested
elements, but then I find that there are not valid values or
attributes for those elements. I wanted a quick way to purge out
those elements. You are correct that I know my data structure and I
could structure the XML around the data. I wanted to keep my XML
class generic for reuse. Here is an example of what I would like to
do:

<element1>
<element2></element2>
<element3 attr1="attr1"></element3>
</element1>

I would like to remove produce the resulting XML:
<element1>
<element3 attr1="attr1"></element3>
</element1>

Thanks again for your input...
Nov 12 '05 #3
T Conti wrote:
The situation I am running into is that I write several nested
elements, but then I find that there are not valid values or
attributes for those elements. I wanted a quick way to purge out
those elements. You are correct that I know my data structure and I
could structure the XML around the data. I wanted to keep my XML
class generic for reuse. Here is an example of what I would like to
do:

<element1>
<element2></element2>
<element3 attr1="attr1"></element3>
</element1>

I would like to remove produce the resulting XML:
<element1>
<element3 attr1="attr1"></element3>
</element1>


There is no such built-in functionality. You could write custom
XmlWriter, which skips empty elements. That's not rocket a engineering
indeed.

--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #4
"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message news:<uV**************@TK2MSFTNGP10.phx.gbl>...
T Conti wrote:
The situation I am running into is that I write several nested
elements, but then I find that there are not valid values or
attributes for those elements. I wanted a quick way to purge out
those elements. You are correct that I know my data structure and I
could structure the XML around the data. I wanted to keep my XML
class generic for reuse. Here is an example of what I would like to
do:

<element1>
<element2></element2>
<element3 attr1="attr1"></element3>
</element1>

I would like to remove produce the resulting XML:
<element1>
<element3 attr1="attr1"></element3>
</element1>


There is no such built-in functionality. You could write custom
XmlWriter, which skips empty elements. That's not rocket a engineering
indeed.

Thanks...
Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chris | last post by:
Hi, I'm stumped and could use some suggestions. I'm trying to serialize some data to an XML fragment in string form, then deserialize it from a string back to an XML fragment, from which I...
3
by: Pete | last post by:
I'm trying to read an XML document and write out a slightly modified version using the XmlTextWriter. I'm basically trying to copy all the nodes exactly as they are read and do some text...
12
by: AFN | last post by:
I am running the code below to generate XML from a data table. But some fields in the data table are Null for every record. Suppose field5 has a null database value. I would expect to see: ...
1
by: Lee Atkinson | last post by:
Hi - I am trying to read an XML file using XmlTextReader and write it out using XmlTextWriter. The XML is XHTML. At the moment, I am trying to create a straightforward copy of a file. The XHTML...
1
by: Dan | last post by:
I'm trying to write an XmlDocument to disk using an XmlTextWriter. Everything is fine except that I can't get the XML declaration to come out. I'm using the code below. What am I doing wrong? ...
2
by: JD | last post by:
Hi -- When I create an xml data stream using C# XMLTextWriter I can use WriteStartElement/WriteElementString and WriteEndElement. Works great. Now I'm trying to add elements to an existing piece...
2
by: Mori | last post by:
I am using the following code to manufactue xml using xmltextwriter. The xml is okay except for the extra set of start document and the outer <string> element. Where do they come from and how do...
2
by: Oenone | last post by:
I am using an XmlTextWriter to create an XML document, which we then send to a client. The document is failing to validate against the DTD we are providng when the load it into their...
4
by: John Salerno | last post by:
I thought I might use the XML functions in C# to help me do some repetitive typing in an XHTML file, but I'm stuck. Here's what I have before I just stopped: void WriteXMLFile() { string path...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
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...
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...
0
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 ...
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.