467,136 Members | 1,235 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,136 developers. It's quick & easy.

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
  • viewed: 1609
Share:
8 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Wolfgang Kreuzer | last post: by
3 posts views Thread by IMS.Rushikesh@gmail.com | last post: by
9 posts views Thread by jab | last post: by
reply views Thread by Atos | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.