.NET Forum / .NET Framework / XML / April 2006
Create a single xml node
|
|
Thread rating:  |
David Thielen - 07 Apr 2006 04:51 GMT 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.
 Signature thanks - dave david_at_windward_dot_net http://www.windwardreports.com
Steven Cheng[MSFT] - 07 Apr 2006 09:59 GMT 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 - 22 Apr 2006 18:45 GMT 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?
 Signature thanks - dave david_at_windward_dot_net http://www.windwardreports.com
> Hi Dave, > [quoted text clipped - 27 lines] > > This posting is provided "AS IS" with no warranties, and confers no rights. David Thielen - 22 Apr 2006 18:52 GMT Hi;
And in UTF-8 for the node value if at all possible.
 Signature thanks - dave david_at_windward_dot_net http://www.windwardreports.com
> Hello; > [quoted text clipped - 41 lines] > > > > This posting is provided "AS IS" with no warranties, and confers no rights. Steven Cheng[MSFT] - 24 Apr 2006 04:14 GMT 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.
 Signature Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.)
David Thielen - 24 Apr 2006 04:22 GMT 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.
 Signature thanks - dave david_at_windward_dot_net http://www.windwardreports.com
> Thanks for your followup Dave, > [quoted text clipped - 21 lines] > (This posting is provided "AS IS", with no warranties, and confers no > rights.) Steven Cheng[MSFT] - 24 Apr 2006 07:26 GMT 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.
 Signature Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.)
David Thielen - 24 Apr 2006 23:19 GMT 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); }
 Signature thanks - dave david_at_windward_dot_net http://www.windwardreports.com
Steven Cheng[MSFT] - 25 Apr 2006 02:47 GMT 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.
 Signature Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.)
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|