简体   繁体   English

如何仅从XML文件中获取文本?

[英]How to get only a text from a XML file?

I tried this code: 我尝试了这段代码:

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string xmlString = System.IO.File.ReadAllText(@"d:\adilipman1937067724.xml");

            XmlDocument doc = new XmlDocument();
            doc.Load(xmlString);
            string t = doc.InnerText;

            textBox1.Text = t;
        }
    }
}

But getting error: 但是出现错误:

error: Invalid URI: The Uri string is too long. 错误:无效的URI:Uri字符串太长。 The file im trying to read is xml of chat history in my messenger with my brother. 我试图读取的文件是我和我的兄弟在Messenger中的聊天记录的xml。 The file size is: 492kb . 文件大小为:492kb。

Getting error exception message: 正在获取错误异常消息:

System.UriFormatException was unhandled
  Message=Invalid URI: The Uri string is too long.
  Source=System
  StackTrace:
       at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
       at System.Uri..ctor(String uriString, UriKind uriKind)
       at System.Xml.XmlResolver.ResolveUri(Uri baseUri, String relativeUri)
       at System.Xml.XmlUrlResolver.ResolveUri(Uri baseUri, String relativeUri)
       at System.Xml.XmlTextReaderImpl..ctor(String url, XmlNameTable nt)
       at System.Xml.XmlDocument.Load(String filename)
       at WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e) in D:\C-Sharp\AnimatedGifEditor\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 25
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at WindowsFormsApplication1.Program.Main() in D:\C-Sharp\AnimatedGifEditor\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Couple of things. 几件事。

Easiest way to see whether the format of an xml document is correct is to open it in Internet Explorer. 查看xml文档格式是否正确的最简单方法是在Internet Explorer中打开它。 It'll tell you if there are problems. 它会告诉您是否有问题。

An easy way to view the contents of an xml document, with all xml tags stripped away: 一种查看xml文档内容的简单方法,其中删除了所有xml标签:

XmlDocument doc = new XmlDocument();
doc.Load(@"C:\tt.xml");
string xmlString = doc.InnerText;

The above method will also tell you if there problems with your xml document. 上面的方法还将告诉您xml文档是否存在问题。

The syntax of your call looks correct, so I assume there must be a problem with your xml document. 您的调用语法看起来正确,因此我认为您的xml文档肯定有问题。 Fix that first (one problem at a time!), then have another go. 首先解决该问题(一次解决一个问题!),然后再解决另一个问题。

Firstly the compile error you are getting is correct, you need to close your string literal. 首先,您得到的编译错误是正确的,您需要关闭字符串文字。

string xmlString = System.IO.File.ReadAllText(@"C:\tt.xml"); 

The method that you are using reads all the text from the file so of course you see the tags !! 您使用的方法会从文件中读取所有文本,因此您当然会看到标签!

If you just want the node values then load the xml into an XmlDocument and use the InnerText property. 如果只需要节点值,则将xml加载到XmlDocument中并使用InnerText属性。

        var doc = new XmlDocument();
        doc.Load(@"C:\tt.xml");
        var str = doc.InnerText;

The variable str will contain the text, minus the xml tags. 变量str将包含减去xml标记的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM