Avatar of Sha1395
Sha1395
 asked on

Message Format xml

my application get a message from msmq and populate in datagrid view at the same time i show up in message box also.

All my messages are XML messages (<> tag based).

while (Enum.MoveNext())
                    {
                        mes = Enum.Current;
                        mes.Formatter = new XmlMessageFormatter(new string[] { "System.String,mscorlib" });
                        m = mes.Body.ToString();
                      messagebox.show(m);
                        }

Open in new window


messagebox.show(m); now show messages eg: <Header><child></child></Header>

I just want to show a message in my message box like
<Header>
<Child>
</child>
</header>

any help please
C#

Avatar of undefined
Last Comment
Sha1395

8/22/2022 - Mon
BourbonKid

I understand your problem as being one of presentation, that is the message you retrieve is all on a single line and you would prefer it to have one tag on each line. Correct me if I'm wrong.

Unfortunately, carriage returns and line feeds are irrelevant as long as XML is concerned. More precisely they are treated as spaces and thus ignored.

A solution would be to :

1°) Retrieve the raw body of the message.
2°) Deserialize the body into custom class instances you would have defined. Deserialization would be performed using the XmlSerializer.Deserialize() method.
3°) Create an instance of XmlWriterSettings and adjust its property to your needs (use NewLineOnAttributes property)
4°) Create a MemoryStream instance
5°) Create an XmlWriter instance (using Create method) and provide both the settings from 3° and the stream from 4° as arguments.
6°) Create a new XmlSerializer then invoke the Serialize method on the root object of your message and provide the writer from 5°) to the method.
7°) Reset position of stream from 4° to the beginning, read the full content of the stream and translate to a string using whathever Encoding is required.

That's it. However isn't it a little bit overkill ?
6°)
Sha1395

ASKER
you got my problem correct BourbonKid.

you might seen in my code already am formatting the message ,all i want is just read the whole string and put new line after every tag.
ASKER CERTIFIED SOLUTION
BourbonKid

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
kaufmed

Line 2 should be:

    using System.IO;
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Sha1395

ASKER
i can't thank enough to you guys Kaufmed and bourbonkid for your great help