0

I have this problem:

I have a method

private  XmlElement ToXmlElement(string xml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            return doc.DocumentElement;
        }

And my input xml string is:

<?xml version="1.0"?>
<Collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:IEEE-1671:2009.02:Common">
<Item name="edsw">
<Collection />
</Item>
</Collection>

I need to return from this string XmlElement, which looks like:

<Collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:IEEE-1671:2009.02:Common">
    <Item name="edsw">
    <Collection />
    </Item>
    </Collection>

thats everything without <?xml version ="1.0"?> but doc.DocumentElements returns me only:

<Item name="edsw" xmlns="urn:IEEE-1671:2009.02:Common">
<Collection />
</Item>

is there any way how to achieve it?

4
  • That does not happen to me. DocumentElement returns the whole Collection. How are you converting the result to string? Commented Sep 4, 2011 at 12:33
  • when I am debuggin it i see in DocumentElement inner xml only this, not whole collection, Commented Sep 4, 2011 at 12:38
  • string ToStringElement(XmlElement xml) {StringWriter sw = new StringWriter(); XmlTextWriter writter = new XmlTextWritter(sw); xml.WriteTo(writter); return sw.toString(); } Commented Sep 4, 2011 at 12:39
  • 1
    After fixing your typos, ToStringElement(ToXmlElement(xml)) returns the whole Collection. That means ToXmlElement() actually does what you want it to do. Commented Sep 4, 2011 at 12:45

1 Answer 1

2

Look at the OuterXml, that's where you will find what you need:

doc.DocumentElement.OuterXml
Sign up to request clarification or add additional context in comments.

2 Comments

but thats only string, I need to return XmlElement
@Martin Ch, well you already have it: doc.DocumentElement, that's what you are returning. When you are debugging look at the OuterXml property and not the InnerXml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.