简体   繁体   中英

Retrieving XML from a C# function on WCF

I have a WCF service that I'm using for calling a C# function via URL, this is the code that I use for the url:

[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml, UriTemplate = "?GetValuesVar={Var}")]

string getHisUmsTimeRedVar(string Var); 

Once donde that I'm using the parameters of the URL for calling a function and to retrieve a value. My problem is that all the XML code is between <string></string> tags.

<string>
<?xml version="1.0"?><ArrayOfHistoricVal
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<HistoricVal>
<Date>2016-05-31T22:00:00</Date><Value>2060</Value>
</HistoricVal>
</ArrayOfHistoricVal>
</string>

Is there a way to retrieve this values as a normal XML instead of between those two <string></string> tags?

Edit: The values to get are in a function like this one:

public string getHisUmsTimeRedVar(string idVar)
        {
            try
            {
                ADAMUtil.Log.info("getHisUmsTimeRed. Empieza = ");
                DateTime FechaIni = new DateTime(2015, 05, 05, 0, 0, 0);
                DateTime FechaFin = new DateTime(2018, 05, 05, 0, 0, 0);

                List<HistoricVal> list = new List<HistoricVal>();

                List<His> Historicos = null;
                ADAMUtil.Log.info("getHisUmsTimeRed. Empieza1 = ");
                Historicos = getHisUmsTimeRed("admin", "admin", Convert.ToInt64(idVar), 14, null, 0, 0, FechaIni, FechaFin, 0, "Romance Standard Time");
                ADAMUtil.Log.info("getHisUmsTimeRed. Historicos = " + Historicos.Count);
                for (int i = 0; i < Historicos.Count; i++)
                {
                    HistoricVal item = new HistoricVal();
                    item.Date = Historicos[i].IniDat;
                    item.Value = Historicos[i].Val;
                    list.Add(item);
                }


                String xmlDoc;
                xmlDoc = toXML.VartoXML(list);
                //Browser.BrowserOpen();
                return xmlDoc;
            }

            catch (Exception ex)
            {
                ADAMUtil.Log.info("getHisUmsTimeRed. ex = " + ex.ToString() + ex.StackTrace.ToString());
                return null;
            }
        }

And the webinvoke must be an Stream, not a string.

[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml, UriTemplate = "?GetValuesVar={Var}")]

stream getHisUmsTimeRedVar(string Var); 

The problem with your XML string is that it is erroneous XML: the XML declaration should be at the very start. You can't easily convert it to a XElement or XDocument like this.

There are thus two solutions:

  • Change your method so that it doesn't return a string, but a XElement in the first place.
  • Work with your string yourself to remove the additional tags. This solution is not robust to change or edges cases, and I recommend you use the first one. But if you are unable to change the called method, this will at least help you out :

     //Trims 8 characters (<string>) at the beginning and 9 at the end. String correctString = yourString.Substring(8, yourString.Length - 17); 

EDIT: After discussing the issue in the comments, it seemed that only the browser displayed the <string> tags, and they were not present in the XML file. This was due to it serializing your string object, most likely. By either serializing it yourself and passing this to your browser with XElement.Parse() or changing the type of your object directly, the browser was able to understand what it was supposed to display.

Change

public string getHisUmsTimeRedVar

To

public XDocument getHisUmsTimeRedVar

to return proper xml not involved in a string tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM