Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi im trying to get nodes values from web service which is work only when i return a string
but when i return object give me an error

What I have tried:

[WebMethod]
        public  book  find(string id)
        {
            
            XElement xelement = XElement.Load("books.xml");

           
            IEnumerable<XElement> listOfBooks = xelement.Elements();
        
            
            
            foreach (var _books in listOfBooks)
            {
                if(_books.Element("title").Value==id )
                {
                    this._book.genre= _books.Element("genre").Value;
                  
                    this._book.title= _books.Element("title").Value;
                    this._book.description= _books.Element("description").Value;                  
                }
               
            }
            return _book;

And in my main form
BooksSoapClient obj;
        public Form1()
        {
            
            InitializeComponent();
            obj = new BooksSoapClient();
        }
     

        private void button1_Click(object sender, EventArgs e)
        {

            book bk=(book) obj.find(textBox1.Text);
            label1.Text = bk.author;
        }
Posted
Updated 22-Nov-18 0:35am
Comments
[no name] 22-Nov-18 6:29am    
You want all the node value of the xml?
Member 13990122 22-Nov-18 6:52am    
yes i can get the values but i wanted to return an group of nodes as object !
but the problem not sure how to reuse the object .
book bk=(book) obj.find(textBox1.Text);
[no name] 22-Nov-18 6:53am    
Try the below code which will return the list of node object
Member 13990122 22-Nov-18 6:59am    
! Thanks , still its not the problem,
if you look at my code how i suppose to re use the obj had been returned from find function.
Member 13990122 22-Nov-18 6:57am    
no only that match my id
then return all as obj , but couldn't figure out how re use the obj

1 solution

Try with this. It will give all node name and node value.
<?xml version="1.0" encoding="utf-8" ?>
<placeholders>
	<JobCode>Enter job code</JobCode>
	<ParentJobCode>Enter parent job code</ParentJobCode>
	<Name>Enter name</Name>
	<JobType>Select job type</JobType>
	<MonitorInterval>Enter monitor interval</MonitorInterval>
	<ThreadSleepTime>Enter thread sleep time</ThreadSleepTime>
	<NoOfThread>Enter no of thead</NoOfThread>
	<TimeZoneDifference>Enter time zone</TimeZoneDifference>
	<Remarks>Enter remark</Remarks>
	<SMTPServer>Enter SMTP server</SMTPServer>
	<EmailBody>Enter email body</EmailBody>
	<SendInterval>Enter send interval</SendInterval>
	<NoOfTimes>Enter no of times</NoOfTimes>
</placeholders>




 public class GenericModel
 {
    public string NodeName{ get; set; }
    public string NodeValue { get; set; }
 }




List<GenericModel> placeHolder = new List<GenericModel>();
XmlDocument doc = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath("~/Content/Help/placeholder.xml"));
  XmlElement root = doc.DocumentElement;
  XmlNodeList nodes = root.SelectNodes("/placeholders");
    foreach (XmlNode node in nodes)
    {
       foreach (XmlNode child in node.ChildNodes)
       {
          placeHolder.Add(new GenericModel()
          {
              NodeName = child.Name,
              NodeValue = child.InnerText
          });
       }
    }

var obj= placeHolder.Find(r => r.NodeName== textBox1.Text);
 
Share this answer
 
v2
Comments
Member 13990122 22-Nov-18 6:55am    
hithanks for answer i can collect which nodes , as object like i wrote above ,
but couldnot reuse the object when i call obj.find(textBox1.Text); , i find my nodes but still cant take them speart.
[no name] 22-Nov-18 7:02am    
I have improved the solution. Please check.
var obj= placeHolder.Find(r => r.NodeName== textBox1.Text)
It will give you the object.
Member 13990122 22-Nov-18 7:07am    
thats true ,
if i have a list of obj,
and return the list ! how i can reuse it , get list parameter!?
[no name] 22-Nov-18 7:10am    
You have a list of object. You want to find the specific object from the list. Then simply use this linq.
var obj= placeHolder.Find(r => r.NodeName== textBox1.Text)
Member 13990122 22-Nov-18 7:09am    
[WebMethod]
public List<string> find(string id)
{

XElement xelement = XElement.Load("books.xml");


IEnumerable<xelement> listOfBooks = xelement.Elements();
var retList = new List<string>();
foreach (var _books in listOfBooks)
{
if(_books.Element("title").Value==id )
{
//this._book.genre= _books.Element("genre").Value;
retList.Add(_books.Element("genre").Value);
retList.Add(_books.Element("title").Value);
retList.Add(_books.Element("description").Value);
//this._book.title= _books.Element("title").Value;
//this._book.description= _books.Element("description").Value;
}

}
return retList;

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900