【问题标题】:How to read XML document and display the output as string in c#如何在 C# 中读取 XML 文档并将输出显示为字符串
【发布时间】:2012-03-30 15:05:17
【问题描述】:

考虑一下我的源文件看起来像这样。

        <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 文件并将其显示为字符串。

谁能帮忙解决这个问题。

谢谢你, 穆齐米尔。

【问题讨论】:

    标签: c# asp.net xmldocument xmlreader


    【解决方案1】:

    您需要OuterXml 属性:

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

    另外你正在加载一个文件而不是xml所以使用

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

    而不是

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

    【讨论】:

      【解决方案2】:

      您真的必须反序列化 XML 吗?为什么不直接将其作为文本文件读取呢?有点像..

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

      显然有适当的错误处理..

      【讨论】:

        【解决方案3】:

        我会尝试使用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

        【讨论】:

          【解决方案4】:

          如果您只想将文件写入输出,可以使用Response.WriteFile

          【讨论】:

            【解决方案5】:

            试试这个

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

            【讨论】:

              【解决方案6】:
              String text = File.ReadAllText(Server.MapPath("~/App_Data/sample.xml"));
              txtData.Text = text;
              

              【讨论】:

                猜你喜欢
                • 2019-04-14
                • 2022-01-15
                • 1970-01-01
                • 2014-01-16
                • 2017-09-11
                • 2023-03-27
                • 2016-11-05
                • 1970-01-01
                • 2012-10-19
                相关资源
                最近更新 更多