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 9314865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:14:36+00:00 2026-06-19T02:14:36+00:00

I am using Linq To XSD to send data over MSMQ. Here is my

  • 0

I am using Linq To XSD to send data over MSMQ. Here is my sending code

public void Send()
        {
            string criMessageQueuePath = ConfigurationManager.AppSettings["CRIMessageQueuePath"];

            if (!MessageQueue.Exists(criMessageQueuePath))
            {
                MessageQueue.Create(criMessageQueuePath, false);
            }

            var messageQueue = new MessageQueue(criMessageQueuePath) { Label = "CRI Message Queue" };
            messageQueue.Formatter = new XmlMessageFormatter(new[] { typeof(XML) });

            var transaction = new MessageQueueTransaction();

            transaction.Begin();

            messageQueue.Send(CreateAuction(1), transaction);
            messageQueue.Send(CreateAuction(2), transaction);
            messageQueue.Send(CreateAuction(3), transaction);
            messageQueue.Send(CreateAuction(4), transaction);
            messageQueue.Send(CreateAuction(5), transaction);

            transaction.Commit();
        }

        private XML CreateAuction(int id)
        {
            var message = new XML {id = id};
            return message;
        }

Here is my receiving code

public IEnumerable<string> Receive()
        {
            string criMessageQueuePath = ConfigurationManager.AppSettings["CRIMessageQueuePath"];

        var messageQueue = new MessageQueue(criMessageQueuePath);

        Message[] messages = messageQueue.GetAllMessages();

        foreach (Message message in messages)
        {
            message.Formatter = new XmlMessageFormatter(new[] { typeof(XML) });
            yield return message.Body.ToString();
        }

        messageQueue.Purge();
    }

I’ve tried lots of variations of this code but can’t figure out why I can’t get a valid XML object out the other end.

Currently, my code is failing when I call it

static void Main()
        {
            var sender = new Sender.Sender();
            sender.Send();

            var receiver = new Receiver.Receiver();
            foreach (var xml in receiver.Receive())
            {
                var typedXML = XML.Parse(xml);
                Console.WriteLine(typedXML.id);
            }
        }

The line it fails on is var typedXML = XML.Parse(xml);. The error is

Element is not an instance of type Domain.XML

The XSD for the object is

<?xml version="1.0" encoding="Windows-1252"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="Domain">
  <xs:element name="XML">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="id" type="xs:int" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Looking at the value of xml on the line

var typedXML = XML.Parse(xml);

I can see that it generates this

"<XML>\r\n  <XML xmlns=\"Domain\">\r\n    <id>1</id>\r\n  </XML>\r\n</XML>"

In other words it generates an additional xml node around the whole xml. If I remove this extra node in the debugger then it all works. what am I doing wrong?

  • 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-06-19T02:14:38+00:00Added an answer on June 19, 2026 at 2:14 am

    This is the result of using hte XmlMessageFormatter as it serializes your object and wraps it in XML. I would suggest that you write your XML document directly to the BodyStream property instead.

    var msg = new Message();
    var writer = new StreamWriter(msg.BodyStream);
    
    writer.Write(xmlDoc.ToString());
    writer.Flush();
    
    queue.Send(msg);
    

    This way you have full Control over what is sent in your queue. The formatters are mostly there for legacy use and should not be used when sending XML documents as messages.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im using LINQ over DataSet but just to get data from some tables, by
Using LINQ on collections, what is the difference between the following lines of code?
using Linq-to-SQL I'd like to prefetch some data. 1) the common solution is to
using LINQ to XML, this is a sample of my XML <shows> <Show Code=456
Using Linq to call a stored procedure that passes a single string, The stored
Using LINQ can I get the data from the table into the format shown
I am using Linq-to-XML to work with an xsd file (I am treating it
Using LINQ to XML, how can I join two sets of data based on
Using LINQ how to select from a List within a List public class Model
using LINQ i am query a data for sorting and i am getting right

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.