Making an XML mess, oh.. message

  • Thread starter Thread starter christery
  • Start date Start date
C

christery

Trying (or has accompished) a XML message by instanitated a class and
fed it to the xmlwriter, now the receiver dont like my (MS?) XML and
want me to code it by hand... *mumbles*
Can I edit a class with some [mumbojumbo] to enter xml handles as I
please or?
put the root declaration with that, but namespace and objectid is
inherited from my code... They (consultant) build it from scratch just
like a string and send it... but I dont see the point, im sending a
instanceiated object (that cant be spelld right) so I do it this way,
if I change the class the message will change automaticly ->less 4 me
to worry about..

Wish I could post the code, but not at work so... just bugging me that
I cant solve it nicely..
//CY
 
Trying (or has accompished) a XML message by instanitated a class and
fed it to the xmlwriter, now the receiver dont like my (MS?) XML and
want me to code it by hand... *mumbles*
Can I edit a class with some [mumbojumbo] to enter xml handles as I
please or?
put the root declaration with that, but namespace and objectid is
inherited from my code... They (consultant) build it from scratch just
like a string and send it... but I dont see the point, im sending a
instanceiated object (that cant be spelld right) so I do it this way,
if I change the class the message will change automaticly ->less 4 me
to worry about..

Could you give an example of:
- class
- XML you get
- XML you want
?

Arne
 
Trying (or has accompished) a XML message by instanitated a class and
fed it to the xmlwriter, now the receiver dont like my (MS?) XML and
want me to code it by hand... *mumbles*
Can I edit a class with some [mumbojumbo] to enter xml handles as I
please or?
put the root declaration with that, but namespace and objectid is
inherited from my code... They (consultant) build it from scratch just
like a string and send it... but I dont see the point, im sending a
instanceiated object (that cant be spelld right) so I do it this way,
if I change the class the message will change automaticly ->less 4 me
to worry about..

Could you give an example of:
   - class
   - XML you get
   - XML you want
?

Arne

So, they want something like:

<?xml version="1.0" standalone="yes"?>
<MESSAGE>
<HEADER>
<Identity>633361405884157414</Identity>
<Sender>TRANSPONDERSYSTEMET</Sender>
<Reciever>LUPP</Reciever>
<Type>VAGNDATA</Type>
<Test>N</Test>
</HEADER>
<VAGNDATA>
<TaggId>00059F</TaggId>
<VagnAgare>0837</VagnAgare>
<VagnsNummer>447217154</VagnsNummer>
<VagnSida>H</VagnSida>
<RSSI>0007</RSSI>
<TidPunkt>2008010808:2248</TidPunkt>
</VAGNDATA>
</MESSAGE>

And what (not nearly completed) I send look like
<?xml version="1.0" encoding="utf-8"?><vagn xmlns:xsi="http://
www.w3.org/2001/X
MLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="JvgAdjToL upp"><RSSI>1234</RSSI><vagnId>3245</vagnId></vagn>


Thinking along the lines of using for example:

using System;
using System.Xml.Serialization;

namespace Console_Test
{
[XmlRootAttribute(Namespace = "JvgAdjToLupp", IsNullable =
false)]
public class vagn
{
private string _RSSI;
private string _vagnId;

public String RSSI
{
get { return _RSSI; }
set { _RSSI = value; }
}

public String vagnId
{
get { return _vagnId; }
set { _vagnId = value; }
}
}
}


yes I know its not the same (not even close, but I didnt have the code
with me) but just the first line standalone=yes... where in *el* did
that come from?

//CY
 
Here's a useful trick for you; put your desired xml in a file,
test.xml.

Now load the VS command prompt (in the start menu) and execute (note
you might need to specify the path):

===
xsd test.xml
xsd test.xsd /classes
===

The first line creates test.xsd from your test.xml; the second line
creates test.cs from your test.xsd; this gives you an example of how
you can generate the desired output with attributes (heck, you could
just use this test.cs "as is") - for example:


[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Identity {
get {
return this.identityField;
}
set {
this.identityField = value;
}
}

If you want to change the markup at the start of the xml (or the
spacing etc), you can do this via XmlWriterSettings - for example:

XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
using(XmlWriter writer = XmlWriter.Create(path, settings))
{
serializer.Serialize(writer, obj);
writer.Close();
}

Marc
 
Here's a useful trick for you; put your desired xml in a file,
test.xml.

Now load the VS command prompt (in the start menu) and execute (note
you might need to specify the path):

===
xsd test.xml
xsd test.xsd /classes
===

OK, I just serialized the instance of the class into xml something
like this example:

String XmlizedString = null;
MemoryStream memoryStream = new MemoryStream ( );
XmlSerializer xs = new XmlSerializer ( typeof
( Animal ) );
XmlTextWriter xmlTextWriter = new XmlTextWriter
( memoryStream, Encoding.UTF8 );

xs.Serialize ( xmlTextWriter, pObject );
memoryStream = ( MemoryStream ) xmlTextWriter.BaseStream;
XmlizedString = UTF8ByteArrayToString
( memoryStream.ToArray ( ) );


But I had to loose the double UTF8 encoding... got some strange char
in the beginning..
This just get pObject and write it to a memorystream but i lose a bit
of control, its just the instance of the class that is XML coded

Your way might be better, then I have control over produced code via
the definition xsd
//CY
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top