1

を使用して Web サービスを作成しましたC#が、そのメソッドの 1 つに XML を返すようにしたいと考えています。私はそうすることができましたが、すべてのデータはタグ付けされてCDATAおり、解析されていません。それは私が探しているものではありません。

これは私のコードです:

 [WebMethod(EnableSession = true, Description = "Returns the safe activities for the required days period in XML")]
    public string GetSafeActivitiesXML(string safename, int days, string FileName)
    {
        string returnErrorCode = "001";
        try
        {
            XmlWriterSettings settings = new XmlWriterSettings
            {
                Indent = true
                //IndentChars = "  ",
                //NewLineChars = "\n",
                //NewLineHandling = NewLineHandling.None,
                //Encoding = System.Text.Encoding.UTF8
            };

            StringWriter sb = new StringWriter();
            XmlWriter writer = XmlWriter.Create(sb,settings);

            writer.WriteStartDocument();
            writer.WriteStartElement("GetSafeActivitiesResult", "");

            int lineCouner = 0;

            if (safeActivities.Count > 0)
            {
                writer.WriteStartElement("ListOfStrings", "");
                foreach (ActivityLogRecord activity in safeActivities)
                {
                        writer.WriteStartElement("string");
                        writer.WriteElementString("outFileName", (activity.Info1.Substring(activity.Info1.LastIndexOf("\\")+1)));
                        writer.WriteElementString("activityTmStamp", activity.Time.ToString());
                        writer.WriteElementString("userName", activity.UserName);
                        writer.WriteElementString("ActionID", activityCode);
                        writer.WriteElementString("direction", direction);
                        writer.WriteElementString("path", activity.Info1);
                        writer.WriteEndElement();
                        lineCouner++;
                    }
                 }
                writer.WriteEndElement();
            }

            writer.WriteStartElement("retunCode");
            writer.WriteString((lineCouner > 0) ? "0" : "2");
            writer.WriteEndElement();
            writer.WriteStartElement("retunMessage");
            writer.WriteString((lineCouner > 0) ? "תקין" : "אין נתונים");
            writer.WriteEndElement();

            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();

            XmlDocument xmlOut = new XmlDocument();

            xmlOut.LoadXml(sb.ToString());
            writer.Close();
            //xmlOut.Save(xxx);
            string finalOutput = sb.ToString();
            finalOutput.Replace("![CDATA[", "").Replace("]]", "");
            return sb.ToString();

        }
        catch (Exception ex)
        {
            this.LogWrite("GetSafeActivities", string.Format("Operation has failed: {0}, internal errorcode: {1}", ex.Message,returnErrorCode), Session.SessionID, true);
            return string.Format("<ReturnCode>{0}</ReturnCode><ReturnMSG>{1}</ReturnMSG>", "שגוי", ex.Message) ;             
        }

    }

以下は、現在の出力の例です。

 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
  <GetSafeActivitiesXMLResponse xmlns="http://www.securenet.co.il">
     <GetSafeActivitiesXMLResult><![CDATA[<?xml version="1.0" encoding="utf-16"?>
  <GetSafeActivitiesResult>
   <ListOfStrings>
<string>
  <outFileName>code-xmp-tmp.txt</outFileName>
  <activityTmStamp>21/06/2015 10:58:38</activityTmStamp>
  <userName>naaman</userName>
  <ActionID>קובץ אוחסן בכספת</ActionID>
  <direction>Unknown</direction>
  <path>Root\fgdf\code-xmp-tmp.txt</path>
</string>
</ListOfStrings>
<retunCode>0</retunCode>
<retunMessage>תקין</retunMessage>
 </GetSafeActivitiesResult>]]></GetSafeActivitiesXMLResult>
   </GetSafeActivitiesXMLResponse>
</soap:Body>

これは私が達成したいものです:

 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
  <GetSafeActivitiesXMLResponse xmlns="http://www.securenet.co.il">
     <GetSafeActivitiesXMLResult><?xml version="1.0" encoding="utf-16"?>
  <GetSafeActivitiesResult>
   <ListOfStrings>
<string>
  <outFileName>code-xmp-tmp.txt</outFileName>
  <activityTmStamp>21/06/2015 10:58:38</activityTmStamp>
  <userName>naaman</userName>
  <ActionID>קובץ אוחסן בכספת</ActionID>
  <direction>Unknown</direction>
  <path>Root\fgdf\code-xmp-tmp.txt</path>
</string>
</ListOfStrings>
<retunCode>0</retunCode>
<retunMessage>תקין</retunMessage>
 </GetSafeActivitiesResult></GetSafeActivitiesXMLResult>
   </GetSafeActivitiesXMLResponse>
</soap:Body>

だから私の質問は、CDATAタグを取り除く方法と、そもそもなぜそこにあるのかということです。

私はxmlが初めてなので、しばらくお待ちください。

4

2 に答える 2

0

メソッドは文字列型を返しましたが、それが問題でした。戻り値の型を XmlDocument に変更したところ、すべてハチミツとナッツになりました。

于 2015-06-30T10:43:32.663 に答える
0

達成したい出力は整形式でないXMLです: 基本的に、XML ドキュメント (またはフラグメント) 内のリテラル文字データとして、 XML 宣言(つまり)を備えた XML ドキュメントをネストしようとしています。法的な構造ではありません。<?xml version="1.0" encoding="utf-16"?>

XML ドキュメント、またはmarkupとして認識される任意のテキストを別の XML ドキュメント (要素) 内に含める適切な方法は、基本的にCDATAセクションを使用してエスケープし、マークアップとして解析されないようにすることです。そしてそれこそが、Web サービス/SOAP インフラストラクチャーがあなたのために行っていることです。

それが行われず、XML テキストが必要に応じて解析されたデータ ( PCDATA) になった場合、コンシューマー パーサーは例外をスローするか、エラーを返します。これは、Web サービス応答 XML が整形式でないためです。

于 2015-06-30T08:17:22.400 に答える