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
1876 Views

Similar Articles

[PageSpeed] 0

Reply:

Similar Artilces:

XmlSerializer Collection with Collections
Hello I got this working but it is not how I really want it, basically I have an xml file which has a root of <test> and can be filled with 3 different types of <question> elements with different attributes, all share a base set of 4, one of the question types can have children with <option> elements, this is how the xml looks after serialization.... If you notice there is an extra <SelectionList> around the <option>'s in the final <question>, how can I write my class so I don't have this extra <SelectionList> element....my class follows, you ...

how do i plot a rose diagram to show frequency of wind direction
can i use excel or another program in microsoft to do this? Hi Kathryn, Get a wind rose for Excel here http://edferrero.m6.net/content/windrose.zip Ed Ferrero http://edferrero.m6.net "kathryn" <kathryn@discussions.microsoft.com> wrote in message news:71874D4A-0D49-4490-BAE1-2C74F9E876FC@microsoft.com... > can i use excel or another program in microsoft to do this? ...

direct link to outlook / outlook express
RMS should include a direct link to Outlook to alow for better customer email outs. also. better customer labels, similar to the item label setup with a label designer. ---------------- This post is a suggestion for Microsoft, and Microsoft responds to the suggestions with the most votes. To vote for this suggestion, click the "I Agree" button in the message pane. If you do not see the button, follow this link to open the suggestion in the Microsoft Web-based Newsreader and then click "I Agree" in the message pane. http://www.microsoft.com/Businesssolutions/Communi...

Rule to direct mail from only 1 of a user's email addresses
My exchange account has a 3 email addresses. I would like to direct mail to a specified folder based on which of the 3 email addresses it was sent "to." When I configure a rule, email from all 3 of my addresses are copied to the specified folder. How can I exclude the other to email addresses that the mail may be coming "to" from this rule. Thanks. Nick "Nick Mirro" <nickmirro@gmail.com> wrote in message news:92538e47-1dd1-46f2-ad46-66e311b68b28@t39g2000prh.googlegroups.com... > My exchange account has a 3 email addresses. I would like to direct &...

XmlSerializer
I'm having a basic problem with the XmlSerializer & i'm at my wits end trying to sort it out i have a basic clas <Serializable()> Class Tes Public Name as Strin End Clas when this serializes i wan <Test><Name>Value</Name></Test the problem is if i use the XmlSerializer clas Dim t as Tes t.Name="Value Dim sb As New StringBuilde Dim sw As New StringWriter(sb Dim Serializer As New XmlSerializer(Message.GetType Serializer.Serialize(sw, t Serializer = Nothin Console.Write(sb.ToStr...

XmlSerializer and shared objects (or How to generate IDREFs using XmlSerializer)
This is a multi-part message in MIME format. ------=_NextPart_000_007F_01C34AC1.11D4FB20 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable I am trying to find a solution that will allow me to use XmlSerializer = to serialize/deserialize a collection of objects where a given object is = shared between two or more other objects, and not create duplicate XML = representations of the shared object, but instead use IDREFs to refer to = the shared object. The XML I'm trying to produce is as follows (where "href" is an IDREF): <...

Macro to toggle move selection after entry direction
Using XL2002, I have a form to fill in which requires the entry of quite a few numbers. The form is designed so the numbers are entered acrross columns instead of down rows. It would speed up the entry if i could enter a number, then press "Enter", and have the cell to the right selected instead of the cell below. Is there a macro where i can toggle between moving the selection right or moving it down? It would sure speed up my work! Thanks... Tonso Not necessary. Tools>options>edit>>>>> -- Don Guillett Microsoft MVP Excel SalesAid Software dguillett1@austin.r...

EU Working Time Directive
Has anybody found an appropriate template for tracking working hours as per EU Working Time Directive, primarily for the transport industry. ...

XmlSerializer and inherited objects
Hi I have a hirachy of classes which are Message(base), then FileMessage and ChatMessage (extended) I want to serialize the objects and when i am deserizaling i dont know if i am getting FileMessage or ChatMessage. So how to get that object and use it I have written following code for serialization public void Send(Message message) { NetworkStream netWorkStream=null; try { XmlSerializer serializer=new XmlSerializer(message.GetType()); netWorkStream=new NetworkStream(_clientSocket); Stream stream=(Stream)netWorkStream; serializer.Serialize(stream,message); } finally { netWorkStrea...

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"); ...

Can Direct Deposit Statements be re-printed after checks has been
Is it possible to re-print Direct Deposit statements in GP after a payroll has been posted? -- Thanks for your help Hi-- Unfortunately, they cannot. Your best option is to look at printing a check history or earnings history for the employee. Take care, Christina Thanks for your help, I believe this will work. -- Thanks for your help "turtlesoup00@hotmail.com" wrote: > Hi-- > > Unfortunately, they cannot. Your best option is to look at printing a > check history or earnings history for the employee. > > Take care, > Christina > > . > ...

Opposite direction
Assume I have numbers in cells A1:A5 and B1:B5. I would like to add them in column C, but following this formula: C1=A1+B5, C2=A2+B2,...,C5=A5+C1. How do I do it so that I can just use copy paste of this formula without manually re-entering it in every cell? Thanks. Branko Try this formula in C1 and then copy it down... =A1+INDEX(B1:B5,6-ROW(A1)) -- Rick (MVP - Excel) "Branko Pecar" <bpecar1@sbcglobal.net> wrote in message news:eXYI0K%23aKHA.5608@TK2MSFTNGP05.phx.gbl... > Assume I have numbers in cells A1:A5 and B1:B5. I would like to add them ...

XMLSerializer Serialize
Iam trying to create a xml file by serializing a custom business object. Iam using a xmltextwriter, but the problem there are invalid characters at the end of the generated file anyone knows a way around it ? tia Dim myStream As New IO.MemoryStream Dim myXMLWriter As New System.Xml.XmlTextWriter(myStream, System.Text.Encoding.UTF8) myXMLWriter.Formatting = System.Xml.Formatting.Indented Dim myShipments As OrderShipments myShipments = CType(myOrder.OrderAddresses(0), COrderAddress).Shipments myShipments.ToXML(myXMLWriter) ...

XmlSerializer help
Hi, I have a class: public class Note { public string name; public string information; } And i Serialize this class, but want to Serialize it to an XmlDocument which will contain many of these elements. How do i do this? Thank you, Tim. Timothy, take a look at Chris Lovett's XmlNodeWriter on www.gotdotnet.com [0]. Simply pass the node where you want to insert the serialized XML into the XmlDocument to an XmlNodeWriter and pass the XmlNodeWriter to the Serialize method. -- HTH Christoph Schittko [MVP] Software Architect, .NET Mentor [0] http://gotdotnet.com/Communit...

problem inheriting from XmlDocument and XmlElement
hello there everyone.. I have created a class called "ProcessLog" inheriting from XmlDocument and two classes ("UploadedItem", "ProcessError") inheriting from XmlElement. I then have two classes ("UploadedTemplate", "UploadedPresentation") which inherit from "UploadedItem")... so far so good. I have overriden the CreateElement method of "ProccessLog" and it creates the correct elements as expected. The problem is I need to override SelectSingleNode and SelectNodes to return types "UploadedPresentation", &qu...

some help or direction on using excel
cutting to the chase. what i'm trying to do is i have a report that i import from one program to an excel spreadsheet, based on the information in that report i need to be able to send email notifications to people on the report to make sure they are going to be able to meet their deadline. the report gives me a code for the persons name and i have a vlookup function i built to turn that code into the persons name. I dont need to send the report to everyone. just those people who are close to missing their deadline. my fields are something like "ID", "loc...

Making Calls directly from Outlook
Hi! I'm using the O2 PDAphone which runs on Microsoft Mobile. I connect it to my laptop and syncs it with Outlook most of the time while I am in the office. I have a colleague who's using a Sony Ericsson phone, and she is able to place calls directly through Outlook using the 'Call Contact' function. Is there any way I can do the same? I find that very convenient, instead of searching through my contacts on the PDAphone manually. Has anyone done it before, please enlighten me. Thanks to all! CQ ...

Directly link item engineering data rev level to PO entry/update
Common industry standard is to note the revision level of the item ordered on the PO... this is required for ISO. There is no direct table link from item engineering data to the PO Entry/update regarding item's current revision level. -- Steve Laurenzano K-Rain Manufacturing ---------------- This post is a suggestion for Microsoft, and Microsoft responds to the suggestions with the most votes. To vote for this suggestion, click the "I Agree" button in the message pane. If you do not see the button, follow this link to open the suggestion in the Microsoft Web-based News...

Problems with XmlSerializer and namespaces
I have an object, Teacher, that has a namespace. Let's call it http://foo. Teacher has a child object called Name, composed of String first, middle, last, etc. Problem I have is when I try to deserialize it. When I deserialize a single Teacher, it works when I put the namespace on the Teacher element. However, if I have just the namespace on Teacher, it will deserialize fine as Teacher. *However*, if I try to deserialize an ArrayOfTeacher, Teacher.Name doesn't deserialize (it's null); without the array, it's OK. What I wound up doing was serializing both a Teacher ...

XmlSerializer and ICollection
Hello all, I have two simple classes: Item and ItemCollection. Item stores a label for the item and an instance of ItemCollection for all child items. ItemCollection just stores a collection of Items. ItemCollection implements System.Collections.ICollection. When I use XmlSerializer to serialize an instance of ItemCollection, I am unable to control the element names used to contain the item collection and the items therein. Applying XmlRootAttribute to the ItemCollection class causes an exception because it implements ICollection. I have also tried implementing IXmlSerializer in my ItemColl...

XmlSerializer question
Hi, I use XmlSerializer to serialize xml string. It escapes xml special charaters, like < and > to < and >. How can I stop XmlSerializer to escape these charaters? Thanks in advance!!! Cindy Cindy wrote: > I use XmlSerializer to serialize xml string. It escapes xml special > charaters, like < and > to < and >. How can I stop XmlSerializer to escape > these charaters? If the XmlSerializer didn't escape the characters < and & (and for compatibility >), then the result would not be well-formed XML. See section 2.4 of the XML specification [1]...

hotmail account directed to exchange
hi, couple of questions: is it possible to create just a mailbox in exchange with no user in AD? is it possible to divert Hotmail or any POP emails to exchange for a user? if so how can this be achieved please. thanks guys. Z In news:1168033276.868523.286650@i15g2000cwa.googlegroups.com, mcp <zafarhussain@blueyonder.co.uk> typed: > hi, > > couple of questions: > > is it possible to create just a mailbox in exchange with no user in > AD? No - a mailbox is "part" of a user account. > > is it possible to divert Hotmail or any POP emails to exchang...

XmlSerializer and DefaultValue?
Hello, I'm currently playing around with the XmlSerializer to replace my old handmade Xml parsing code, but am having a few small issues with default values and derived classes. Say we have classes A, B and C: abstract class A { protected int a_value = 1; [XmlAttribute("value"), DefaultValue(1)] public int AValue { get { return a_value; } set { a_value = value; } } } class B : A { .... } class C : A { .... } Now, I want class B and C to have different default values, more precisely, I want B to have 1 as default value and C to have Int32.MaxValue. But I ...

Sudden Loss of Direct Statements
My citi credit card statement used to synch just pefectly with money 2003 dx on my Win XP machine. Suddenly, it stopped. I've checked with the bank, and the account was locked because of "too many incorrect login" attempts. After unlocking, I changed the password and tried loging in via the web. That works. Using same information in money yields nothing except lockouts. I've verified the login information several times. It just simply doesn't seem to work from money. (I've got 9 other direct synching accounts from this file that still work just fine...) Anyone ev...

XmlSerializer question...
Hi All, When using XmlSerializer is there anyway of keeping "xsi", "xsd" namespaces and PI appearing in the xml file ? <?xml version="1.0"?> <myClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="Test" Visible="false" /> Just want a simple clean one like the following... <ColumnInfo Name="Test" Visible="false" /> Thanks, Maersa wrote: > When using XmlSerializer is there anyway of keeping "xsi", "xsd"...