How to XmlSerialize directly into XmlDocument

Hi

this approach works IF the target document is empty:

XmlDocument doc = new XmlDocument();
XPathNavigator nav = doc.CreateNavigator();
XmlSerializer ser = new XmlSerializer(typeof(string));
using (XmlWriter writer = nav.AppendChild()) {
    ser.Serialize(writer, "works ok");
}//using

but if I want to insert the xml into an existing element:

XmlDocument doc = new XmlDocument();
doc.AppendChild(doc.CreateElement("Root"));
XPathNavigator nav = doc.FirstChild.CreateNavigator();
XmlSerializer ser = new XmlSerializer(typeof(string));
using (XmlWriter writer = nav.AppendChild()) {
    ser.Serialize(writer, "fails");
}//using

giving message "WriteStartDocument cannot be called on writers created
with ConformanceLevel.Fragment."

Does anybody know of a workaround?
0
first101 (1)
9/27/2010 4:44:31 PM
dotnet.xml 7266 articles. 0 followers. Follow

0 Replies
2057 Views

Similar Articles

[PageSpeed] 10

Reply:

Similar Artilces:

XmlSerializer with object derived from CollectionBase
I have been looking into using the MS application block for configuration management, and I am very happy with the options it provides. Since it stores hashtables, I decided that for configuration data my app needs, I would create strongly typed collections for each config type. For instance, CarCollection dervied from CollectionBase which only accepts items of Car would be set to hashtable key "AppCars". I am running into all kinds of hurdles trying to get the hashtable serialized. The app block takes care of converting the hashtable to an XmlNode collection I believe, but I...

Print Barcodes w Eltron 2742 direct thermal (legacy)
When I upgraded to RMS 1.3 (from QuickSell2000) tech told me all would be wonderful. Now my old Eltron 2742 direct thermal does not print. Bought and returned Zebra TLP2844 because we could not get it to work. Tech left country but he had the old eltron (never the new zebra) printing barcodes through rms (I believe as a windows printer-not using rms printer wizard). Got the seagullscientific.com printer driver for Eltron and installed but does not solve problem. (Zebra people say they bought eltron and while they no longer support it, they told me the seagullscientific driver will make...

HowTo: Generating several namespaces for an element in an XmlDocument?
Hi everybody, I'm using VS 2005, .Net 2.0. The CreateElement() method of XmlDocument allows to supply a namespace URI. Now I want to add several namespace URIs with different prefixes to the element. How do I do that. Here an example: <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> I could do this with...

XMLSerializer
I have XMLSerializer successfully serializing a collection class that inherits CollectionBase. My reason for doing it this way instead of using an XMLArrayAttribute is that I need to be able to add an XMLAttribute at the group level. Although the group object and child objects serialize fine to XML, the extra property I added to the collection class is ignored. I've tried declaring it with and without the <XmlAttributeAttribute()> tag, but it is ignored in both cases. I need it to be serialized as an attribute of the group tag. Is it possible to do that with a collection class? ...

suppressing xsd namespace declarations with XmlSerialize?
Hi... I'm just starting to play around with xml serialization. Is there any way to suppress the xsd and xsi declarations in the resulting xml? It doesn't appear that they're used but they come out anyway. Thanks -Mark Hi Mark, We need to perform more research on this issue . We will reply here as soon as possible. If you have any more concerns on it, please feel free to post here. Thanks for your understanding! Best regards, Wen Yuan Hi Mark, Thanks for your wait. If you don��t need xsi and xsd declaration with xml serialization, you can use XmlSerializerNamesp...

Direction
I am looking for some direction as to which Dynamics Products are going to fit what I am looking to do? I am looking for an application (main goal is communication) that will link our sales and professional services departments. Here are some of the tactical things that I looking for out of the system: Sales - Forecasting - Automatic e-mail updates to all concerned parties when opportunities, tasks or projects change Services - Ability to track Blocktime, T&M and Projects - Web Time Entry - Customer Services portal capability (having a customer log a ticket via the wed, then we...

how to return a xmldocument to classic asp
Hi everyone, I'm making a SOAP request to a webservice from classic asp with(SOAP Toolkit v3.0). This function has to return the XML document or the xmlstring to the classic asp page. How can i do this? Here is how i try todo it now: Public Function getData(ByVal strParamValue As String) As System.Xml.XmlDocument Dim connIsOpen As Boolean Dim strXml As String Dim xmlDoc As New XmlDocument() 'Dim xmlNodlist As XmlNodeList strParamValue = strParamValue + "%" connIsOpen = openConnection() If connIsOpen Then ...

UpdateAllViews() across threads <ducks to avoid objects thrown in my direction>
First off, yes I know all about the issues involved in calling any MFC function that affects an HWND across threads. I know all about the thread-local storage of the HWND -> CWnd* maps, etc. I know all about the concurrency issues in modifying shared data. I have read MSDN and "Advanced Windows" extensively. That said, I want to call UpdateAllViews() in a separate thread...or at least find another solution that DOES NOT involve posting messages. Here's the scenario... I have a DLL that does some stuff and reports back periodically (about every 50 ms) through a call...

Single direction Synchronization with Access
I need advice on the issue of synchronization of the Outlook Calendar with an Access database. I have experience programming DAO with Access objects but not with the Outlook objects. Here is my situation: I have an access application with a scheduling feature (amongst many other features). I want to offer the users a nice calendar interface to see the events that they insert in the application. I think it would be best to send this information to Outlook and offer my users to see their data there, rather than re-inventing the wheel and trying to build a calendar interface of my own in Access. ...

XmlSerializer: deserialize against xsd generated class
Hi, I have a schema that has an optional element, fieldTag4000Field. If the element is omitted from the XML request, when it is deserialized, it will be null when I check it - which is fine. What happens when the element is supplied as <fieldTag4000Field/> (empty), it does not equate to null. I want to be able handle this at the deserialization level rahter than in my edits later. Is there a way to alter the behavior so when I deserialize it, it is null? I also want to add that I am calling the "CanDeserialize" method of the XmlSerializer object, passing in the reque...

Void PR Direct Deposit Payment
We are trying to void a payroll direct deposit. We select the checkbook ID but are unable to bring up the DD number to void. We try to select by Employee ID, Check Number and Audit Trail code but we get "There are no checks in this range to void for this Checkbook ID and year". The check date is in the current year (2008). The check/DD number is not a duplicate number and it has not been reconciled in the checkbook. The number does exist in both the PR Check and Transaction tables. Why can't we see it in the Void Payroll Checks window? You can't have a duplicate c...

XmlDocument.Load throwing error saying CR must be followed by LF
We have an application using Dim doc As New XmlDocument doc.Load(VariableForUNCFilePath) in VB.NET 2.0 to load an XML document for operating on the nodes thereof. This was functioning wonderfully up until yesterday. It worked at 10:20 AM EST on 02/10/2009, but at the next operation (3:42 PM EST) it didn't work anymore. Now, when I attempt to load the same documents that had loaded previously using this same code as before, I get the error "The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF". Anyo...

Why is some mail going directly to 'delete' box
Hi, I'm using Outlook 2000 (SP3). I recently discovered that a couple of incoming e-mail addresses go directly to the 'delete' box. These are email addresses I never would have put into the "junk e-mail" section (and I checked there to be sure), and I didn't give them any specific 'rules'. Why would some emails automatically go to the deleted box? Thanks, james, Seattle Remove the spamblock from my address if emailing me. Open Outlook ==> Click on Tools ==> Organize and check the Junk email settings and adult content settings. Also you might wan...

XmlDocument.LoadXml Errors with &amp in string
I have a project in which I get xml posted via a hidden form field (data=<xml></xml>). People are entering names like Bill & Sandy Smith. this is coming over as: <NameInfo><FamilyName><FamilyNames>David &amp; Karen</FamilyNames><Surname>Adams</Surname></FamilyName></NameInfo> When I try to load this up in a XmlDocument it blows up. string xmlData = Request.Form["RequestData"]; dom.LoadXml(xmlData); results with this: "This is an unexpected token. The expected token is 'EndElement'. Line 1, position...

direct emails are not created
when using the direct email option nothing seems to happen after running the option. no activities.....no emails. Are there any tricks with direct emails? thanks I think - you just need to be patient - there is a lag while the async service gets on with creating and sending them "Steve" <Steve@discussions.microsoft.com> wrote in message news:B0EDD97D-B0BA-42B0-B422-9EA4364BBCCB@microsoft.com... > when using the direct email option nothing seems to happen after running > the > option. no activities.....no emails. Are there any tricks with direct > emails? >...

Dialing Contacts directly from Outlook
I would love to be able to have Outlook automatically dial my contacts (rather than looking up someon's phone #) and then manually dialing it. It appears that the software is able to do this. My laptop is equipped with a modem. My question is: how do I set this up? Simply stated, what gets plugged in where? I have a high speed cable internet connection and digital land line telephone service from the same provider. Would greatly appreciate any suggestions! -- Phil D Outlook is not a dialer. It simply hands off to the Windows dialer which has always has required an an...

I keep losing online services on my HSBC direct savings account
I have lost online services on my HSBC account 4 times now in the 4 months I have been using MS Money (MS Money 2006 Standard OEM Edition Version 15.0.50.513). I think I have finally figured out the pattern. Every time I accidentally screw up something I can't undo and I have to restore a backup file it happens. If I restore a backup everything is back to how it should be except that it just drops the online services from my HSBC account, and I have to set them back up, which creates a duplicate account that now has online services that must then be merged with the old account. ...

Concise directions on separating database front end and back end??
I have a database application that is currently being used by 30-40 users. It has been running successfully for the last 6 months, but I know from reading the posts here that I should separate the front end from the back end. I would appreciate it if someone would post CONCISE (easy to understand) instructions on how this should best be accomplished. This is the largest application I've worked with and the first time I've used one in a multi-location environment. Currently, everyone is using the same mdb file which resides on a network server. This server is physically located i...

Faster way to reconstruct XmlDocument
Hello, I have profiled my code, and the current performance bottleneck is this routine. It gets called 7400 times to do some intensive calculations when a user uses the feature. The xmlText parameter will be used to pass in a xml document text stored in db, the text contents (about 200 of these guys total) are stored in a cached static variable. My web app must be thread-safe, so I cannot create and cache 200 XmlDocument objects because each thread will do differently to the object it uses including modifying its content. 1) Is there another implementation that runs faster than what I cur...

openning directly a meeting request
Hi everybody ! In a Web Intranet Application, I would like : 1. to open by a web link a new Meeting Request 2 add one or several people to this meeting request If I use OWA : 1. I can open with this url http://<server>/exchange/<user>/Calendar/?Cmd=new&mm=11&dd=5&yy=2003 2. Is it possible to add people ? If I use Outlook XP 1. I can just open a window with the calendar using <a href="outlook:calendar">link</a> but I can no more open a new meeting request as we could do it with outlook 2000 like this : href...

Not able to send email directly from CRM...
Using MS CRM 3.0 on SQL 2005. Email Router is installed on Exchange Server 2003. When I send an email from CRM, it goes to History record with Email ID however it is not actually transmitted to the recipient. We can however send the email through Outlook and track the email to CRM. Can anyone give a clue why we are not able to send email directly from CRM interface. We must be missing something. Can someone please help me in resolving this issue? We are not able to send email from CRM. Tia did you configure your SMTP settings when you did the crm install? Has email never worked or has...

XMLDocument <- Synchronization Trasformation Schema -> XMLDocument ?
I'm new to .NET XML classes, so here my dilemma I really like the XMLDataDocument concept of synchronizing automatically XMLDocument and DataSet with a common schema. Can I automatically syncronize 2 XMLDocument with Synchronization Trasformation Schema in the middle? Here my plan I'm going to write an application which the core data model is based on XMLDataDocument with my own schema. The application frontend will edit the XMLDataDocument core. The application will have to edit files with a well known XML document schema similar to my core. I can easily create an XSLT to tranform t...

removing nodes from xmldocument
Here is an xml structure. i want to remove <a> nodes that do not have any child. How can i do that in csharp? <root> <a> <b/> </a> <a/> <a/> <a> <c/> </a> </root> i tried following but it does not work: xmlNodeList l = xr.getElementsByTagName("a"); foreach(XmlNode n in l) { if(n.HasChildNodes == false) xr.RemoveChild(n) } emid, Try this call this function inside the code; void RecurssionRemoval() { XmlNodeList l = xr.GetElementsByTagName("a"); ...

Opening and altering XML-doc using XmlDocument wrecks havoc on doc
Hi I am trying to post-process some XML-documents from some third-party software. I open them, find the right element, manipulate the text and write it back to the element. Dim x As New Xml.XmlDocument Dim nl As Xml.XmlNodeList Dim text As String Dim textOutput As String x.Load(filename) nl = x.GetElementsByTagName("text") [snip, some manipulation of the text, not important...] nl.Item(0).InnerText = "<![CDATA[ " & textOutput & " ]]>" 'nl.Item(0).InnerText = textOutput x.Save("C:\Temp\XmlYt.xml") Now, the data I write ...

XmlSerializer operator: why does it generate an assembly?
I've noticed that when I instantiate the XmlSerializer that it generates, compiles and loads an assembly. Is this really necessary? How can this be performant? Is there any way around this? XmlSerializer serializer = new XmlSerializer(a.GetType()); ...