Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5959483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:41:28+00:00 2026-05-22T18:41:28+00:00

I’m having a bit of trouble retrieving body from a wcf message. I am

  • 0

I’m having a bit of trouble retrieving body from a wcf message. I am trying to implement WCF message inspector to validate messages against XSD schema.

The soap body looks like following:

<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Header xmlns="http://www.test1.com">
      <applicationID>1234</applicationID>
    </Header>
    <GetMatchRequest xmlns="http://www.tempuri.org">test</GetMatchRequest>
  </s:Body>

The problem is when I try to get body it only gets partial body message. Gets only header element, ignores GetMatchRequest element(may be because of multiple namespaces…)

I am using following to get message body:

XmlDocument bodyDoc = new XmlDocument();
bodyDoc.Load( message.GetReaderAtBodyContents().ReadSubtree());

I have also tried following:

bodyDoc.Load( message.GetReaderAtBodyContents());

The code above results in error – This document already has a ‘DocumentElement’ node.

Can anyone please help in extracting body from a WCF message?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:41:29+00:00Added an answer on May 22, 2026 at 6:41 pm

    Message.GetReaderAtBodyContents return the reader positioned not at the element, but at its first child. Usually the message body contains only a single root element, so you can load it directly. But in your message it contains multiple root elements (Header and GetMatchRequest), so if you want to load the whole body in a XmlDocument, you need to provide a wrapping element (the XmlDocument can have only one root element). In the example below I use <s:Body> as the wrapping element, but you could use anything you want. The code simply reads the body until it finds the end element (</s:Body>).

        public class Post_a866abd2_bdc2_4d30_8bbc_2ce46df38dc4
        {
            public static void Test()
            {
                string xml = @"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
      <s:Body xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
        <Header xmlns=""http://www.test1.com"">
          <applicationID>1234</applicationID>
        </Header>
        <GetMatchRequest xmlns=""http://www.tempuri.org"">test</GetMatchRequest>
      </s:Body>
    </s:Envelope>";
                Message message = Message.CreateMessage(XmlReader.Create(new StringReader(xml)), int.MaxValue, MessageVersion.Soap11);
                Console.WriteLine(message);
                XmlDocument bodyDoc = new XmlDocument();
                MemoryStream ms = new MemoryStream();
                XmlWriter w = XmlWriter.Create(ms, new XmlWriterSettings { Indent = true, IndentChars = "  ", OmitXmlDeclaration = true });
                XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents();
                w.WriteStartElement("s", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
                while (bodyReader.NodeType != XmlNodeType.EndElement && bodyReader.LocalName != "Body" && bodyReader.NamespaceURI != "http://schemas.xmlsoap.org/soap/envelope/")
                {
                    if (bodyReader.NodeType != XmlNodeType.Whitespace)
                    {
                        w.WriteNode(bodyReader, true);
                    }
                    else
                    {
                        bodyReader.Read(); // ignore whitespace; maintain if you want
                    }
                }
                w.WriteEndElement();
                w.Flush();
                Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
                ms.Position = 0;
                XmlDocument doc = new XmlDocument();
                doc.Load(ms);
                Console.WriteLine(doc.DocumentElement.OuterXml);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.