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 ドキュメントに問題があるに違いないと思います。最初にそれを修正し (一度に 1 つの問題を解決してください!)、次にもう一度やり直してください。

于 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 に答える