2012-03-30 9 views
-1

私のソースファイルはこのように見えます。XML文書を読み込み、出力を文字列として表示するには#

 <Content xmlns="uuid:4522eb85-0a47-45f9-8e2b-1x82c78xx920"> 
      <first>Hello World.This is Fisrt field</first> 
      <second>Hello World.This is second field</second> 
    </Content> 

私は場所からこのXMLドキュメントを読んでコードを書くと文字列として表示します。

say name of the xml file is helloworld.xml. 
    Location: D:\abcd\cdef\all\helloworld.xml. 

私は以下を試みましたが、できませんでした。

  XmlDocument contentxml = new XmlDocument(); 
      contentxml.LoadXml(@"D:\abcd\cdef\all\helloworld.xml"); 
      Response.Write("<BR>" + contentxml.ToString()); 

Response.writeには何も表示されません。もし私が何かを見逃したら、私を訂正してください。コンポーネントとエラーを作成しないことが来ています。私もこれを試してみました

  XmlDocument contentxml = new XmlDocument(); 
      try 
      { 
       contentxml.LoadXml(@"D:\abcd\cdef\all\helloworld.xml"); 
      } 
      catch (XmlException exp) 
      { 
       Console.WriteLine(exp.Message); 
      } 
      StringWriter sw = new StringWriter(); 
      XmlTextWriter xw = new XmlTextWriter(sw); 
      contentxml.WriteTo(xw); 
      Response.Write("<BR>" + sw.ToString()); 

をしかし、私は、任意の出力を見つけることができませんでした。

XMLファイルを場所から読み込んでそのまま文字列として表示したいと考えています。

誰でもこれを助けることができます。

ありがとう、 Muzimil。

答えて

4

あなたはOuterXmlプロパティが必要です

Response.Write("<BR>" + contentxml.OuterXml); 

はまた、あなたが簡単にしたい場合はその代わりに

contentxml.LoadXml(@"D:\abcd\cdef\all\helloworld.xml"); 
0

contentxml.Load(@"D:\abcd\cdef\all\helloworld.xml"); 

を使うファイルないXMLをロードしているが出力にファイルを書き込むことができます。Response.WriteFile

0

この

XmlTextReader reader = new XmlTextReader (@"D:\abcd\cdef\all\helloworld.xml"); 
while (reader.Read()) 
{ 
    Console.WriteLine(reader.Name); 
} 
Console.ReadLine(); 
1

はあなたが本当にすべてでXMLをデシリアライズする必要がありますかみては?なぜそれをテキストファイルとして読まないのですか?適切なエラーが明らかに取り扱いに..

String text = File.ReadAllText(@"D:\abcd\cdef\all\helloworld.xml"); 
Response.Write(text); 

ような何か...

1

私はXDocumentクラスを使用してみます:

//load the document from file 
var doc = XDocument.Load("..."); //== path to the file 

//write the xml to the screen 
Response.Write(doc.ToString()); 

あなたの代わりにXmlDocumentを使用したい場合は、あなたがしたいです代わりにLoadを使用してください。LoadXml

0
String text = File.ReadAllText(Server.MapPath("~/App_Data/sample.xml")); 
txtData.Text = text; 
関連する問題