Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a C# class to an XMLElement or XMLDocument

Tags:

c#

xml

I have an C# class that I would like to serialize using XMLSerializer. But I would like to have it serialized to a XMLElement or XMLDocument. Is this possible or do I have to serialize it to a String and then parse the string back to a XMLDocument?

like image 927
Konstantin Avatar asked Dec 22 '09 09:12

Konstantin


People also ask

Can you convert AC Corp to an LLC?

1. Statutory conversion is a relatively new, streamlined procedure, available in many states, that allows you to convert your corporation to an LLC by filing a few forms with the secretary of state's office. Each state that permits statutory conversions has its own specific forms and rules.

What happens when you convert AC Corp to an S corp?

After conversion from a C corp, an S corporation can inherit income such as rent, interest, retained earnings, funds derived from stock sales, etc. Passive income that makes up more than 25% of an S corp's gross income is subject to tax.

Can I change my C corporation to an S corporation?

If your C corporation is eligible for S corporation status, you need to complete IRS Form 2553, Election By a Small Business Corporation. The form needs to be signed and dated by a corporate officer with the authority to sign on the corporation's behalf.

How long does it take to convert from C corp to S corp?

Steps to Convert a C Corporation to an S Corporation All shareholders must sign the form. The timeframe for submitting the form can be no later than two months and 15 days from the beginning of the tax year. This will be the tax year when the S corp election is made.


2 Answers

I had this problem too, and Matt Davis provided a great solution. Just posting some code snippets, since there are a few more details.

Serializing:

public static XmlElement SerializeToXmlElement(object o)
{
    XmlDocument doc = new XmlDocument();

    using(XmlWriter writer = doc.CreateNavigator().AppendChild())
    {
        new XmlSerializer(o.GetType()).Serialize(writer, o);
    }

    return doc.DocumentElement;
}

Deserializing:

public static T DeserializeFromXmlElement<T>(XmlElement element)
{
    var serializer = new XmlSerializer(typeof(T));

    return (T)serializer.Deserialize(new XmlNodeReader(element));
}
like image 130
Dave Andersen Avatar answered Sep 18 '22 08:09

Dave Andersen


You can create a new XmlDocument, then call CreateNavigator().AppendChild(). This will give you an XmlWriter you can pass to the Serialize method that will dump into the doc root.

like image 25
nitzmahone Avatar answered Sep 22 '22 08:09

nitzmahone