我想用  XmlReader读取一个xml文件然后转为 string 返回:public string SendXML()
{
    System.Xml.XmlTextReader xmlThreads = new System.Xml.XmlTextReader("MyXml.xml");
    return xmlThreads.ToString();
}
结果返回的string是“System.Xml.XmlTextReader”,而不是MyXml.xml中的xml内容。

解决方案 »

  1.   

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("test.xml");string s = xmlDoc.InnerXml;
      

  2.   

    不需要解析,只是原样转化为 string 就行了。
      

  3.   

    private void TestIncrementalReadBinHex() {       
       XmlTextReader xmlReader   = new XmlTextReader("wellform.xml");
       try {
          int binhexlen = 0;
          byte[] binhex = new byte[1000];
          while (xmlReader.Read()) {
             if ("dt:BinHex" == xmlReader.Name) break;
          }
          binhexlen = xmlReader.ReadBinHex(binhex, 0, 50);
          for (int i=0; i < binhexlen; i++) Console.Write(binhex[i]);
          while ("dt:BinHex" == xmlReader.Name) {
             binhexlen = xmlReader.ReadBinHex(binhex, 0, 50);
             for (int i=0; i < binhexlen; i++) Console.Write(binhex[i]);
          }
          Console.WriteLine();
          xmlReader.Close();
       }
       catch(Exception e) {
          xmlReader.Close();
          Console.WriteLine("Exception: " + e.Message);
          return;
       }
    }
      

  4.   

    god,那你干嘛要是XmlTextReader来读文件呢直接用TextReader不就可以了,
    读出来后,ReadToEnd就是一个字符串了
      

  5.   

    System.IO.StreamReader sr = new System.IO.StreamReader("");
    string XmlString = sr.ReadToEnd();
    sr.Close();
      

  6.   

    mapserver(杨东 mm小店--http://shop33627662.taobao.com/)是对的,但是开头多了个:
    <string xmlns="http://www.sina.com/Services/WebTutorialServices">
    怎么去掉,用:
    IgnoreSchema  ?
      

  7.   

    chenyuming2004(这辈子我算是废了)的方法也是一样,多了:
    <string xmlns="http://www.sina.com/Services/WebTutorialServices">
    怎么去掉?
    原来的xml文件是没有 <string > </string>的。
      

  8.   

    怎么能去掉 <string xmlns="http://www.sina.com/Services/WebTutorialServices">
     </string>,在线等。因为我要通过网络传输,能减少就尽量减少字符串。
      

  9.   

    几个字节别钻尖了一定要去掉不好办吗?
    Replace("....","")