如何在c#中读取XML文档并将输出显示为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9945585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:19:51  来源:igfitidea点击:

How to read XML document and display the output as string in c#

c#asp.netxmldocumentxmlreader

提问by Patan

consider my source file looks like this.

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

        <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>

I want to write a code, which read this xml document from a location and display it as string.

我想编写一个代码,它从一个位置读取这个 xml 文档并将其显示为字符串。

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

I have tried the following, but i was unable to do it.

我已经尝试了以下,但我无法做到。

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

Response.write is displaying nothing. Correct me if i missed any thing. Its not creating any component and error is coming.

Response.write 没有显示任何内容。如果我错过了什么,请纠正我。它没有创建任何组件,错误即将到来。

I have also tried this,

我也试过这个,

            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());

But i did not find the any output.

但我没有找到任何输出。

I want to read a XML file from a location and display it as it is as string.

我想从某个位置读取 XML 文件并将其显示为字符串。

Can anyone help on this.

任何人都可以帮助解决这个问题。

Thank you, Muzimil.

谢谢你,穆兹米尔。

采纳答案by BrokenGlass

You need the OuterXmlproperty:

你需要的OuterXml财产:

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

Also you are loading a filenot xml so use

此外,您正在加载一个不是 xml的文件,因此请使用

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

instead of

代替

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

回答by Rob Rodi

If you want to simply write a file to the output, you can do Response.WriteFile.

如果您只想将文件写入输出,则可以执行Response.WriteFile

回答by Rizwan Mumtaz

try this

尝试这个

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

回答by Nick Thomson

Do you really have to deserialize the XML at all? Why not just read it as a text file? Something like..

你真的需要反序列化 XML 吗?为什么不将其作为文本文件读取?就像是..

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

With appropriate error handling obviously..

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

回答by James Johnson

I would try using the XDocumentclass:

我会尝试使用XDocument该类:

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

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

If you want to use an XmlDocumentinstead, you would want to use Loadinstead LoadXml.

如果你想用 anXmlDocument代替,你会想用Loadinstead LoadXml

回答by KAPIL SHARMA

String text = File.ReadAllText(Server.MapPath("~/App_Data/sample.xml"));
txtData.Text = text;