Groups | Blog | Home
all groups > dotnet xml > april 2006 >

dotnet xml : Create a single xml node


David Thielen
4/6/2006 8:51:01 PM
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
stcheng NO[at]SPAM online.microsoft.com
4/7/2006 12:00:00 AM
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/default.asp?url=/library/en-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.


David Thielen
4/22/2006 10:45:02 AM
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



[quoted text, click to view]
David Thielen
4/22/2006 10:52:01 AM
Hi;

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

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



[quoted text, click to view]
David Thielen
4/23/2006 8:22:02 PM
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



[quoted text, click to view]
stcheng NO[at]SPAM online.microsoft.com
4/24/2006 12:00:00 AM
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.)
stcheng NO[at]SPAM online.microsoft.com
4/24/2006 3:14:01 AM
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.)
David Thielen
4/24/2006 3:19:02 PM
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

stcheng NO[at]SPAM online.microsoft.com
4/25/2006 1:47:16 AM
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.)
AddThis Social Bookmark Button