|
|
So basically the code below just creates an XML document in memory and then parses it and spits the values back out. The question I have is about the IEnumerable<xelement> statements. I never implemented a class that uses the interface, but yet it still works in this code. Does the compiler automagically create a container or something? How does this work? Does this work for all interfaces?
class Program
{
static void Main(string[] args)
{
XDocument employeeDoc =
new XDocument(
new XElement("Employees",
new XElement("Employee",
new XElement("Name" , "Bob Smith"),
new XElement("Phone" , "408-555-1000"),
new XElement("Phone" , "408-555-1001")),
new XElement("Employee",
new XElement("Name", "Sally Jones"),
new XElement("Phone", "415-555-2000"),
new XElement("Phone", "415-555-2001"))
)
);
XElement root = employeeDoc.Element("Employees");
IEnumerable<XElement> employees = root.Elements();
foreach (XElement emp in employees)
{
XElement empNameNode = emp.Element("Name");
Console.WriteLine(empNameNode.Value);
IEnumerable<XElement> empPhones = emp.Elements("Phone");
foreach (XElement phone in empPhones)
{
Console.WriteLine(" {0}", phone.Value);
}
}
}
}
|
|
|
|
|
See these lines..
Quote: IEnumerable<xelement> employees = root.Elements();
foreach (XElement emp in employees)
{
Here you have used foreach loop. SO, the definition says that, you can use foreach loop only on those objects or classes which implement IEnumerable interface. Therefore, in order to loop out all through the elements, you have taken the IEnumerable instance.
hope it helps.
with regards
Karthik Harve
|
|
|
|
|
Karthik, I'm still a little confused. I know that the foreach statement requires a type that has implemented the IEnumerable or IEnumerator interface, what I don't understand is why the line IEnumerable employees = root.Elements() even works. I didn't implement the IEnumerable interface in a class anywhere. I just used that statement. In the book I'm reading it says that interfaces have to be a part of the classes declaration, for instance:
public class Myclass : IEnumerable
Since I didn't do that, does the compiler make an object with the proper code for me, from the first code example?
|
|
|
|
|
|
|
|
|
|
|
Thank you, Wonde Tadesse.
|
|
|
|
|
Have a look at the definition of the XContainer.Elements method[^]. The type that this returns is an IEnumberable collection. This allows you to run a loop over the elements of the employees object you create. It actually has nothing to do with you implementing or not implementing the interface, since the class XContainer / XElement possibly itself takes care of this internally.
|
|
|
|
|
|
Thank you. This cleared up my confusion.
|
|
|
|
|
You are welcome. Vote if it helped.
|
|
|
|
|
Sorry if anyone gets annoyed by this question please don't mind i am just asking how can i provide good articles what should be the way of it & in which manner.
Please Suggest me something.
Thanks.
|
|
|
|
|
|
|
The first thing that you need to note is that a C# question answer forum.
This question is posted in the wrong forum.
Thus you should remember to write a well-formatted article that is properly categorized in its rightful place and has enough material to be classified as an article.
If you feel your article is too small or does not have enough material, write a tip / trick instead of an article.
|
|
|
|
|
here is my sample code
webservice code
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(Name = "TestService", ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {
// Add a member variable of the type deriving from SoapHeader.
public MyHeader timeStamp;
// Apply a SoapHeader attribute.
[WebMethod(Description = "A method that requires authentication")]
[SoapDocumentMethod(Binding = "TestService")]
[SoapHeader("timeStamp", Direction = SoapHeaderDirection.In)]
public string HelloWorld()
{
if (timeStamp == null)
timeStamp = new MyHeader();
timeStamp.MustUnderstand = true;
timeStamp.Expires = 60000;
timeStamp.Created = DateTime.UtcNow;
return "Hello World";
}
}
here is the client code
localhost.MyHeader header = new localhost.MyHeader();
// Populate the values of the SOAP header.
header.Expires = 60000;
header.Created = DateTime.UtcNow;
// Create a new instance of the proxy class.
localhost.WebService proxy = new localhost.WebService();
// Add the MyHeader SOAP header to the SOAP request.
proxy.MyHeaderValue = header;
// Call the method on the proxy class that communicates
// with your XML Web service method.
string results = proxy.HelloWorld();
i am getting result as "Hello World"
But i want it in format as
?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-; xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<MyHeader xmlns="http://tempuri.org/">
<Created>dateTime</Created>
<Expires>long</Expires>
</MyHeader>
</soap:Header>
<soap:Body>
<HelloWorld xmlns="http://tempuri.org/"
</soap:Body>
</soap:Envelope >
|
|
|
|
|
Don't cross post. Pick one forum and stick with it.
|
|
|
|
|
|
|
You're calling the service method. That means that you want to get the result of the service method. SOAP is a transport mechanism, it's as irrelevant to the service call as whether you use copper or fibre-optic cable to transfer the 1s and 0s.
If you want to see the transport XML for debugging purposes, point a browser at http://localhost/yourservice.svc. If you want to actually pass XML from the service to service clients, you need to return XML from your service methods.
|
|
|
|
|
hi dear
i draw a circle c# with GDI+ function
i set timer on my form
how can i move this circle in my form with this timer
???
thx
|
|
|
|