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

时间:2012-08-22 23:37:36

标签: c# asp.net xml-parsing

我试过这段代码:

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

但是得到错误:

错误:URI无效:Uri字符串太长。我试图阅读的文件是与我兄弟在我的信使中的xml聊天记录。文件大小为:492kb。

获取错误异常消息:

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: 

2 个答案:

答案 0 :(得分:0)

一些事情。

查看xml文档格式是否正确的最简单方法是在Internet Explorer中打开它。它会告诉你是否有问题。

查看xml文档内容的简便方法,删除所有xml标记:

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

上述方法还会告诉您xml文档是否存在问题。

您的调用语法看起来是正确的,因此我假设您的xml文档一定存在问题。首先修复(一次出现一个问题!),然后再做一次。

答案 1 :(得分:0)

首先,你得到的编译错误是正确的,你需要关闭你的字符串文字。

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

您正在使用的方法会读取文件中的所有文本,因此您当然会看到标记!!

如果您只想要节点值,则将xml加载到XmlDocument中并使用InnerText 属性。

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

变量str将包含文本,减去xml标记。