473,385 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Create a single xml node

Hi;

I need to be able to create a single xml node in utf-8 and do it efficiently
as this is called a lot (reason below).

What is the best way to do this? I need to end up with a string like "<node
attr1='dave'>hi there</node>" where &"<>' are all encoded and chars > 127 are
turned into utf-8. I then write this to a file (and what is the best way to
do that)?

Now as to why for the curious. This is for an audit trail and therefore each
node must be written and flushed to disk and the file is opened in append
mode where the permissions on the file allow appending, but not overwriting.

The app can stop and re-start in the middle of the day so when it starts and
we have no audit file, we write <root>. We then add nodes as actions occur.
On the first write after midnight, we write </root>, clode & rename the file,
then open another.

So we can't use XmlWriter as we are not writing a complete xml file and we
need to be able to flush.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Apr 7 '06 #1
8 1858
Hi Dave,

Thank you for posting.

As for writing XML element or append element into XML file, it depends on
the detailed condition. Is the log file very large? If each of the file is
not very large, we can just use XMLDocument class which load the document
into memory and manipulate it. If the file's size will increase to a very
large value, we need to consider using raw XmlWriter to write xml data into
the file. Here is one msdn article discussing on large XML file
manipulating:

#Efficient Techniques for Modifying Large XML Files
http://msdn.microsoft.com/library/de...us/dnxmlnet/ht
ml/largexml.asp

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

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

Apr 7 '06 #2
Hello;

No, I need to be able to create a string like "<item date='1/2/06'>new
data</item>" and then I will write that to the file.

I cannot use the .NET XML classes to write to the file because:
1) The file is append only so it cannot be read or re-written.
2) The file is not correct XML until midnight - up till then I have the
start <root> but not the end </root> and if the system is stopped and
restarted it needs to close the file without adding </root> and then open it
again in append only mode so all existing text in the file cannot be read.

So is there a way to create an XML element and then get it as a string?
--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

"Steven Cheng[MSFT]" wrote:
Hi Dave,

Thank you for posting.

As for writing XML element or append element into XML file, it depends on
the detailed condition. Is the log file very large? If each of the file is
not very large, we can just use XMLDocument class which load the document
into memory and manipulate it. If the file's size will increase to a very
large value, we need to consider using raw XmlWriter to write xml data into
the file. Here is one msdn article discussing on large XML file
manipulating:

#Efficient Techniques for Modifying Large XML Files
http://msdn.microsoft.com/library/de...us/dnxmlnet/ht
ml/largexml.asp

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

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

Apr 22 '06 #3
Hi;

And in UTF-8 for the node value if at all possible.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

"David Thielen" wrote:
Hello;

No, I need to be able to create a string like "<item date='1/2/06'>new
data</item>" and then I will write that to the file.

I cannot use the .NET XML classes to write to the file because:
1) The file is append only so it cannot be read or re-written.
2) The file is not correct XML until midnight - up till then I have the
start <root> but not the end </root> and if the system is stopped and
restarted it needs to close the file without adding </root> and then open it
again in append only mode so all existing text in the file cannot be read.

So is there a way to create an XML element and then get it as a string?
--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

"Steven Cheng[MSFT]" wrote:
Hi Dave,

Thank you for posting.

As for writing XML element or append element into XML file, it depends on
the detailed condition. Is the log file very large? If each of the file is
not very large, we can just use XMLDocument class which load the document
into memory and manipulate it. If the file's size will increase to a very
large value, we need to consider using raw XmlWriter to write xml data into
the file. Here is one msdn article discussing on large XML file
manipulating:

#Efficient Techniques for Modifying Large XML Files
http://msdn.microsoft.com/library/de...us/dnxmlnet/ht
ml/largexml.asp

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

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

Apr 22 '06 #4
Thanks for your followup Dave,

Then, I think you can just create the Node through
XmlDocument.CreateElement or other method, and the XmlNode or XmlElement
class has the "OuterXml" property which can return the complete XML
fragment of that node. You can write it out to the output stream as normal
string value. Does this helps?

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

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

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Apr 24 '06 #5
Hi;

Yes that does it. I was looking at using XmlTextWriter because I figure that
might be more efficient than creating an XmlDocument each time - is
XmlDocument lighter weight?

I was really hoping there was a static call like HttpUtility.HtmlEncode that
would do it in a very lightweight manner.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

"Steven Cheng[MSFT]" wrote:
Thanks for your followup Dave,

Then, I think you can just create the Node through
XmlDocument.CreateElement or other method, and the XmlNode or XmlElement
class has the "OuterXml" property which can return the complete XML
fragment of that node. You can write it out to the output stream as normal
string value. Does this helps?

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

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

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Apr 24 '06 #6
Thanks for your quick response Dave,

I think just create a XmlDocument instance and use it to create some single
Node is not very expensive on performance. The time when we would consider
using XmlDocument expensive is when loading a large xml file or stream into
xmldocument.
And if you use XmlTextReader to do the work(construcing simple node, I
don't think it quite convenient since you need to construct extra stream
instances to work with it.

If you do think using XmlDocument not quite good, you can consider create a
custom utility class with static method to create such simple string based
xml node.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

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

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Apr 24 '06 #7
Ok, I ran some timing tests. This is what I got (100,000 times):

XmlTextWriter: 2.28 seconds
HttpUtility.HtmlEncode: 2.46 seconds
XmlDocument: 3.05 seconds

So the winner is XmlTextWriter

code:
// test
DateTime start = DateTime.Now;
for (int ind = 0; ind < 100000; ind++)
{
StringWriter sw = new StringWriter();
XmlTextWriter xtw = new XmlTextWriter(sw);
xtw.WriteStartElement(ModToNode(mod));
xtw.WriteAttributeString("op", OpToAttr(op));
xtw.WriteString(message);
xtw.WriteEndElement();
xtw.Close();
String str = sw.ToString();
str = null;
}
TimeSpan ts1 = DateTime.Now.Subtract(start);

start = DateTime.Now;
for (int ind = 0; ind < 100000; ind++)
{
String str = " <" + ModToNode(mod) +
" op='" + OpToAttr(op) +
"'>" + TextToAttr(message) +
"</" + ModToNode(mod) + ">";
str = null;
}
TimeSpan ts2 = DateTime.Now.Subtract(start);

XmlDocument doc = new XmlDocument();
start = DateTime.Now;
for (int ind = 0; ind < 100000; ind++)
{
XmlElement elem = doc.CreateElement(ModToNode(mod));
XmlAttribute attr = elem.SetAttributeNode("op", "");
attr.Value = OpToAttr(op);
elem.InnerText = message;
String str = elem.OuterXml;
str = null;
}
TimeSpan ts3 = DateTime.Now.Subtract(start);
....

private static string ModToNode(MODULE mod)
{
return mod.ToString().ToLower().Replace('_', '-');
}

private static string OpToAttr(OPERATION op)
{
return op.ToString().ToLower();
}

private static string TextToAttr(string text)
{
return HttpUtility.HtmlEncode(text);
}

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com
Apr 24 '06 #8
That's fine. I agree that a real test will say many words:)

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

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

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Apr 25 '06 #9

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

Similar topics

7
by: Wolfgang Kreuzer | last post by:
Hello all, I have two tables - Projects and ProjectStruct Table Projects contains master records of the projects, ProjectStruct allows to define a project herarchie and contains the fields...
3
by: IMS.Rushikesh | last post by:
Hi Friends, I need a XPath query, whcih ll return me subset of data.... check below xml stream <label id="MyExpenseDetails_lbl" xlink:role="terseLabel">Short Expense Details</label> <label...
9
by: jab | last post by:
Je veux lier (join) une table qui se trouve dans une database avec une qui se trouve dans une autre database. Les 2 databases sont sur le même serveur en l'occurence DB2/NT 7.2.9. J'ai créé un...
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
5
by: uspensky | last post by:
We are trying to setup a system to system failover cluster using two nodes (x346) which each have a single hba running to seperate controllers on the DS400. For full redundnancy, IBM recommends...
14
by: hgraham | last post by:
Hi, I'm trying to understand how to work the dom, and all I'm trying to do is insert a link right before another link in the html based on it's href value. This isn't a real world example - I'm...
11
by: hotice | last post by:
How to write code to delete a specific node in a single link list that takes O(1) time? £¨ link list uses pointers, not hash. £© That is, the time deleting a node is the same (independent from...
2
by: Dahak | last post by:
It seems that my GoogleFu has failed me tonight and I'm hoping I can find some advice. I've inherited a ASP.NET project written in Visual Basic. I'm not too familiar with the .NET framework...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.