I have this Java code:
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPBody body = message.getSOAPBody();
QName qualifiedName = new QName("http://mysite.com.br/",
"getQuantidade", "ns2");
SOAPBodyElement element = body.addBodyElement(qualifiedName);
SOAPElement codigoProduto = element.addChildElement(new QName("codigo"));
codigoProduto.setValue("ARQ");
SOAPElement quantidade = element.addChildElement(new QName("quantidade"));
quantidade.setValue("2");
message.writeTo(System.out);
I'd like to do the same thing in C#: mount a SOAP envelope steb by step using classes. I did some research and didn't find anything like this.
What would be the best way to do it?
I don't want the exact code as answer, just point me the way (:
Thank you very much.
Related
I am relatively new to programming in C# (Learning on my own for a school project) and decided to try using TweetInvi to implement Twitter functionality.
So far it's going good, got the authentication and publishing up and running, but I'm struggling to find out how to use the DestroyTweet() method.
It, and many other methods takes a tweetID parameter, which I can't figure out of how to find for a specific tweet.
Using the following code to publish a tweet, how can i find the tweetID of this tweet?
public ITweet publishTweet(string text)
{
return Tweet.PublishTweet(text);
}
// Snippet from a test method in main class.
twitter.twitterUser.publishTweet(System.Console.ReadLine());
// Still working on GUI so using ReadLine for now.
It's probably an easy solution, but I just can't figure it out!
Thanks in advance.
You can try something like this:
public string PublishTweet(string text)
{
var appCredentials = new TwitterCredentials(_apiKey,_apiSecret, _accessToken, _accessTokenSecret);
Tweetinvi.Auth.SetCredentials(appCredentials);
text = "my tweet";
var publishedTweet = Tweetinvi.Tweet.PublishTweet(text);
var tweetId = publishedTweet.Id.ToString();
return tweetId;
}
You just need to get the published tweet into a var for the result of the PublishTweet() method then you select the field(s) you need.
Simple solution. As explained before you need to take the tweet back from PublishTweet.
string text = "text";
ITweet tweet = Tweet.PublishTweet(text);
bool destroySuccess = tweet.Destroy();
I'm developing an application using a plugins architecture and I want to send objects between client and server without knowing the type of the object being sent.
Is there a way to send generic data type ?
According to Microsoft pages, the Any field could be an answer to this problem, instead of using a string and a custom serialization/deserialization implementation to send these objects. However, I didn't find the provided c# examples understandable. I tried to solve the problem this way:
ClassTest myClassTest = new ClassTest();
Any packToSend = Any.Pack(myClassTest);
return Task.FromResult(new UnknownTEST
{
Pathm = hai
}); ;
But it seems that I need to implement the IMessage interface in my class and I don't know how to do this.
If anyone could provide a basic example to help me understand how to do this, that would be great.
Thanks !
You need to create protobuf messages which represent the data you're sending. You don't need to create your own classes as you did with your "ClassTest" class.
Here's an example:
point.proto:
syntax = "proto3";
option csharp_namespace = "MyProject.Namespace";
message Point {
int32 x = 1;
int32 y = 2;
}
generic_dto_message.proto:
syntax = "proto3";
import "google/protobuf/any.proto";
option csharp_namespace = "MyProject.Namespace";
message GenericDtoMessage {
google.protobuf.Any data = 1;
}
C# code:
// packing
var point = new Point
{
X = 1,
Y = 22
};
var genericDtoMessage = new GenericDtoMessage();
genericDtoMessage.Data = Any.Pack(point);
// unpacking
var unpackedData = genericDtoMessage.Data.Unpack<Point>();
Console.WriteLine($"X: {unpackedData.X}{Environment.NewLine}Y: {unpackedData.Y}");
Console.WriteLine($"Press any key to continue...");
Console.ReadKey();
In case you are using Grpc.Tools NuGet package to generate C# code for the above written .proto files, don't forget to add this ItemGroup section to your .csproj file:
<ItemGroup>
<Protobuf Include="point.proto" Link="point.proto" />
<Protobuf Include="generic_dto_message.proto" Link="generic_dto_message.proto" />
</ItemGroup>
Hope it helps!
Im still working on my first asp.net core project and now I want to display "a qoute of the day".
I have the qoutes in a xml file stored in a folder called File under wwwroot.
Im planning on my making this a View Component.
Im used to working with web forms so it seems like Im spending alot of time on small issues, but I guess its the only way to learn.
I've created a folder named Custom where I plan to hold all my custom classes. the QuoteController.cs is located in the Controllers folder.
So yeah, I think I know how to crate the View Component. "I think" is an important factor here.
Im also used to using XmlDocument, so Im trying my best to get XmlReader to work. But any hint or tips would be highly appreciated.
This is what I got so far. QuoteController.cs
public class QuoteController : Controller
{
public Custom.Quote Index()
{
Custom.Quote result = new Custom.Quote();
XmlReader rdr = XmlReader.Create(#"\File\qoutes.xml");
Random rnd = new Random(DateTime.Now.Millisecond);
int tmp = rdr.AttributeCount;
int count = rnd.Next(0, tmp);
int i = 0;
while (rdr.Read())
{
if (count.Equals(i))
{
result = new Custom.Quote(rdr.GetAttribute("q"), rdr.GetAttribute("author"));
break;
}
i++;
}
rdr.Dispose();
rdr = null;
rnd = null;
return result;
}
}
I guess the next step will be to add some visuals, but I cant imagine that my code actully works. Does anybody know how to easily parse through and xml file i CORE? Should I go for async?
I guess it doesnt matter, but the xml file is formated like:
<quotes>
<q>Be Strong</b>
<author>Stein The Ruler</author>
</quotes>
Again, I will be very happy if you take the time to look at this :)
Thank you!
My way to implement this:
1)convert the xmldocument to look like this
<quotes>
<quote Content="Be Strong" Author="Stein..."/>
</quotes>
2) Fix the Custom.Quote object to contain these 2 (public getters, setters string) fields: Content and Author,
and then,3) use this code to turn the xml to a list:
XDocument quotesDoc = XDocument.Parse('your path');
List<Custom.Quote> quotes = quotesDoc.Root
.Elements("quote")
.Select(x => new Speaker
{
Content= (string)x.Attribute("Content"),
Author = (string)x.Attribute("Author")
})
.ToList<Custom.Quote>();
Hope this helps!
Hi I have a WCF service which is working good. for testing purpose to QC the data i would like to seriliaze the data and write it to an xml document. how can this be done.
please find the below code where im consuming the WCF service in a client app
Client.EMPServiceClient proxy = new Client.EMPServiceClient();
proxy.ClientCredentials.UserName.UserName = "testuser";
proxy.ClientCredentials.UserName.Password = "password";
Client.EMPSearchCriteria criteria = new Client.EMPSearchCriteria();
criteria.EMPNumber = "01-351";
proxy.GetEMPData(criteria);
Console.Write("Finish");
I wrote a class as below to write the output to a doc - but could some one tell me how to bridge these
public static void SerializeToXML(EMPData pdata)
{
XmlSerializer serializer = new XmlSerializer(typeof(EMPData));
TextWriter txtwriter = new StreamWriter(#"d:\test.xml");
serializer.Serialize(txtwriter, pdata);
txtwriter.Close();
}
Please advice on how to write the output to an xml doc
Thanks,
Justin
Doesn't
proxy.GetEMPData(criteria);
return something? Shouldn't you use that result?
Try...
Client.EMPServiceClient proxy = new Client.EMPServiceClient();
proxy.ClientCredentials.UserName.UserName = "testuser";
proxy.ClientCredentials.UserName.Password = "password";
Client.EMPSearchCriteria criteria = new Client.EMPSearchCriteria();
criteria.EMPNumber = "01-351";
var data = proxy.GetEMPData(criteria); // Change this line
SerializeToXML(data); // and adding this line
Console.Write("Finish");
The right way to do this is with WCF's built-in message logging- no need to modify the app at all. This way, you're sure to get exactly the same message- otherwise your client's serialization can be affected by WCF configuration that won't apply when you serialize the message manually.
http://msdn.microsoft.com/en-us/library/ms751526.aspx
I'm trying to send a SOAP request to a 3rd party web service. I've successfully send and received data from other interfaces in the same service, but I'm having problems with this particular one:
<SP_GoodsMovement xmlns="http://services.hnseu.com">
<GoodsMoved xmlns="http://tempuri.org/SP_GoodsMoved.xsd">
<SerialNumberedGoodsMovements>
<SerialNumbered>
<PartNumber>string</PartNumber>
<SerialNumber>string</SerialNumber>
<MovementType>string</MovementType>
<FromLocation>string</FromLocation>
<FromLocationCategory>string</FromLocationCategory>
<ToLocation>string</ToLocation>
<ToLocationCategory>string</ToLocationCategory>
<AssetMovementTimestamp>dateTime</AssetMovementTimestamp>
<GoodsInReference>string</GoodsInReference>
</SerialNumbered>
<SerialNumbered>
<PartNumber>string</PartNumber>
<SerialNumber>string</SerialNumber>
<MovementType>string</MovementType>
<FromLocation>string</FromLocation>
<FromLocationCategory>string</FromLocationCategory>
<ToLocation>string</ToLocation>
<ToLocationCategory>string</ToLocationCategory>
<AssetMovementTimestamp>dateTime</AssetMovementTimestamp>
<GoodsInReference>string</GoodsInReference>
</SerialNumbered>
</SerialNumberedGoodsMovements>
<NonSerialNumberedGoodsMovements>
<NonSerialNumbered>
<PartNumber>string</PartNumber>
<Quantity>unsignedInt</Quantity>
<MovementType>string</MovementType>
<FromLocation>string</FromLocation>
<FromLocationCategory>string</FromLocationCategory>
<ToLocation>string</ToLocation>
<ToLocationCategory>string</ToLocationCategory>
<Used>boolean</Used>
<AssetMovementTimestamp>dateTime</AssetMovementTimestamp>
<GoodsInReference>string</GoodsInReference>
</NonSerialNumbered>
<NonSerialNumbered>
<PartNumber>string</PartNumber>
<Quantity>unsignedInt</Quantity>
<MovementType>string</MovementType>
<FromLocation>string</FromLocation>
<FromLocationCategory>string</FromLocationCategory>
<ToLocation>string</ToLocation>
<ToLocationCategory>string</ToLocationCategory>
<Used>boolean</Used>
<AssetMovementTimestamp>dateTime</AssetMovementTimestamp>
<GoodsInReference>string</GoodsInReference>
</NonSerialNumbered>
</NonSerialNumberedGoodsMovements>
</GoodsMoved>
</SP_GoodsMovement>
so my code is as follows (i can expand this if necesssary):
...
if (requestType == "SP_GoodsMovement")
{
GoodsMoved SOAP_GoodsMoved = new GoodsMoved();
SOAP_GoodsMoved.SerialNumberedGoodsMovements[0].PartNumber = partNumber[0].InnerXml;
...
string SOAPMessage;
SOAPMessage = request.SP_GoodsMovement(header, SOAP_GoodsMoved).Message;
}
When I run this code I get an 'Object reference not set to an instance of an object' error.
I think i'm not referencing the PartNumber parameter properly, but i've tried a few things without success.
Any ideas?
SOAP_GoodsMoved.SerialNumberedGoodsMovements[0]
doesn't appear to be initialised.
maybe try
GoodsMoved SOAP_GoodsMoved = new GoodsMoved();
SOAP_GoodsMoved.SerialNumberedGoodsMovements = new WhateverObject[1];
SOAP_GoodsMoved.SerialNumberedGoodsMovements[0] = new WhateverObject();
SOAP_GoodsMoved.SerialNumberedGoodsMovements[0].PartNumber = partNumber[0].InnerXml;
or you could right an overload for your GoodsMoved() ctor that ensures that the SerialNumberedGoodsMovements array gets initialized with a certain size.