0

我试过这段代码:

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

2 回答 2

0

几件事。

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

一种查看 xml 文档内容的简单方法,去除了所有 xml 标记:

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

上面的方法也会告诉你你的xml文档是否有问题。

您的调用语法看起来是正确的,所以我认为您的 xml 文档一定有问题。先解决这个问题(一次一个问题!),然后再解决。

于 2012-08-22T23:46:35.950 回答
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 标记。

于 2012-08-22T23:53:51.183 回答