zoukankan      html  css  js  c++  java
  • C#创建XML字符串

    //webService

    using System;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.Xml;
    using System.Text;
    using System.IO;

    [WebService(Namespace 
    = "webservice")] //(Namespace = "http://localhost/webserver/")
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Service : System.Web.Services.WebService
    {
        
    public Service () {

            
    //如果使用设计的组件,请取消注释以下行 
            
    //InitializeComponent(); 
        }


        [WebMethod]
        
    public string HelloWorld() {
            
    return "Hello World";
        }


        [WebMethod] 
        
    public  string  show(string  yourname) 
        

            
    //return  "http://aaaaa"+"欢迎"+yourname;

           
    //生成xml字符串:
            using (StringWriter sw = new StringWriter())
            
    {
                XmlTextWriter xtw 
    = new XmlTextWriter(sw);
                xtw.Formatting 
    = Formatting.Indented;
                xtw.WriteStartDocument();

                xtw.WriteStartElement(
    "root");

                
    //test
                xtw.WriteStartElement("test");
                xtw.WriteString(
    "test content");
                xtw.WriteEndElement();

                
    //test2
                xtw.WriteStartElement("test2");

                
    //testSub
                xtw.WriteStartElement("testSub");
                xtw.WriteString(
    "Sub content");
                xtw.WriteEndElement();

                xtw.WriteEndElement();
            

                xtw.WriteEndElement();
    //root
                xtw.WriteEndDocument();            
              
                
    string result = sw.ToString();

                
    return result.Replace("utf-8""gb2312").Replace("utf-16""gb2312");
            }
     
        }


        
        [WebMethod] 
        
    public string parseXML()
        
    {
            
    ////解析xml:
            string strInput = "<?xml version='1.0' encoding='utf-16'?><foo><bar /></foo>";
            XmlTextReader r 
    = new XmlTextReader(new StringReader(strInput));
            MemoryStream ms 
    = new MemoryStream();
            XmlTextWriter w 
    = new XmlTextWriter(ms, Encoding.UTF8);
            w.WriteNode(r, 
    false);
            w.Flush();
            ms.Position 
    = 0;
            StreamReader sr 
    = new StreamReader(ms);
            
    string strOutput = sr.ReadToEnd();
            
    return strOutput.Replace("utf-8""gb2312").Replace("utf-16""gb2312");
            
    //Console.WriteLine("Input = {0}, Output = {1}", strInput.Length, strOutput.Length);
        }
     
        
    }

  • 相关阅读:
    Python字典推导式将cookie字符串转化为字典
    爬取百度贴吧前1000页内容(requests库面向对象思想实现)
    牛客网:连续子数组的最大和
    在字符串中找出第一个只出现一次的字符,Python实现
    关于时间日期的程序,主要datetime模块
    [读书笔记] Python数据分析 (五) pandas入门
    [学习笔记] CS131 Computer Vision: Foundations and Applications:Lecture 3 线性代数初步
    [读书笔记] Python数据分析 (四) 数组和矢量计算
    [读书笔记] Python数据分析 (三) IPython
    [读书笔记] R语言实战 (六) 基本图形方法
  • 原文地址:https://www.cnblogs.com/ding0910/p/774260.html
Copyright © 2011-2022 走看看