MonoDroid ObjectDisposedException when loading XDocument for RSS - c#

I'm fiddling with MonoDroid (aka Mono for Android) trying to build a simple podcast downloading app, currently just testing out retrieval of an RSS feed contents.
My simple app is:
an edit text where the url for an RSS feed is input
a button to 'download' the rss
a text display for the output of the download operation
When a user clicks on download, I fetch the RSS contents, currently using XDocument (I also tried with just WebRequest / Streams etc.), here's the short version:
void button_Click(object sender, EventArgs e)
{
output.Text += "user input: " + rssUriInput.Text + "\n";
try
{
output.Text += "### document ###";
output.Text += XDocument.Load(rssUriInput.Text).ToString();
output.Text += "### document ###";
}
catch (Exception ex)
{
output.Text += "OOOPS something went wrong:\n" + ex.ToString();
}
}
I've tried several approaches, but keep getting ObjectDisposedExceptions when I do XDocument.Load(/*my uri here*/);
My AssemblyInfo.cs includes the
[assembly: UsesPermission(Android.Manifest.Permission.Internet)]
Now, disclaimer, after getting hello world running I kind of went off on my own, so I may be missing some MonoDroid fundamentals, but this code works in a normal C# app (I tested it) but
Why am I doing wrong here? Do I have to do something special to use xml / linq / network access in MonoDroid?
here is the full activity code:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Xml.Linq;
namespace RssDownlowd
{
[Activity(Label = "RssDownlowd", MainLauncher = true, Icon = "#drawable/icon")]
public class Activity1 : Activity
{
EditText rssUriInput;
Button button;
TextView output;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.RssDownload);
rssUriInput = FindViewById<EditText>(Resource.Id.rssUriText);
button = FindViewById<Button>(Resource.Id.downloadButton);
output = FindViewById<TextView>(Resource.Id.outputTextView);
button.Click += new EventHandler(button_Click);
}
void button_Click(object sender, EventArgs e)
{
output.Text += "user input: " + rssUriInput.Text + "\n";
try
{
output.Text += "### document ###";
output.Text += XDocument.Load(rssUriInput.Text).ToString();
output.Text += "### document ###";
}
catch (Exception ex)
{
output.Text += "OOOPS something went wrong:\n" + ex.ToString();
}
}
}
}
And here is the full exception:
"System.ObjectDisposedException: The object was used after being disposed.
at System.Net.WebConnection.BeginRead (System.Net.HttpWebRequest request, System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback cb, System.Object state) [0x00000] in <filename unknown>:0
at System.Net.WebConnectionStream.BeginRead (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback cb, System.Object state) [0x00000] in <filename unknown>:0
at System.Net.WebConnectionStream.Read (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in <filename unknown>:0
at System.Xml.XmlInputStream.Read (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0
at System.Xml.NonBlockingStreamReader.ReadBuffer () [0x00000] in <filename unknown>:0
at System.Xml.NonBlockingStreamReader.Read (System.Char[] dest_buffer, Int32 index, Int32 count) [0x00000] in <filename unknown>:0
at System.Xml.XmlStreamReader.Read (System.Char[] dest_buffer, Int32 index, Int32 count) [0x00000] in <filename unknown>:0
at Mono.Xml2.XmlTextReader.ReadTextReader (Int32 remained) [0x00000] in <filename unknown>:0
at Mono.Xml2.XmlTextReader.PeekChar () [0x00000] in <filename unknown>:0
at Mono.Xml2.XmlTextReader.ReadChar () [0x00000] in <filename unknown>:0
at Mono.Xml2.XmlTextReader.Expect (Int32 expected) [0x00000] in <filename unknown>:0
at Mono.Xml2.XmlTextReader.ReadEntityReference (Boolean ignoreEntityReferences) [0x00000] in <filename unknown>:0
at Mono.Xml2.XmlTextReader.ReadReference (Boolean ignoreEntityReferences) [0x00000] in <filename unknown>:0
at Mono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace) [0x00000] in <filename unknown>:0
at Mono.Xml2.XmlTextReader.ReadContent () [0x00000] in <filename unknown>:0
at Mono.Xml2.XmlTextReader.Read () [0x00000] in <filename unknown>:0
at System.Xml.XmlTextReader.Read () [0x00000] in <filename unknown>:0
at Mono.Xml.XmlFilterReader.Read () [0x00000] in <filename unknown>:0
at System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0
at System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0
at System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0
at System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0
at System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0
at System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0
at System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0
at System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0
at System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0
at System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0
at System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0
at System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0
at System.Xml.Linq.XDocument.ReadContent (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0
at System.Xml.Linq.XDocument.LoadCore (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0
at System.Xml.Linq.XDocument.Load (System.String uri, LoadOptions options) [0x00000] in <filename unknown>:0
at System.Xml.Linq.XDocument.Load (System.String uri) [0x00000] in <filename unknown>:0
at RssDownlowd.Activity1.button_Click (System.Object sender, System.EventArgs e) [0x0004a] in C:\\Users\\khd483\\code\\HelloMonoDroid\\RssDownlowd\\Activity1.cs:47 "

Strangely enough when I tried your code it worked just fine:
Have you enabled Linking in your project in the Mono for Android options?
Also consider doing an async download of the RSS as it blocks the UI Thread until it is done, making the app unresponsive.

Related

PING ERROR C# - Parameter name: hostName

I need help.. I checking for if user of PC connected to Internet..
It works for me.. and for many people.. But for someone no
and it's writing this into output_log.txt
ERROR:
ArgumentNullException: Argument cannot be null.
Parameter name: hostName
at System.Net.Dns.GetHostByName (System.String hostName) [0x00000] in <filename unknown>:0
at System.Net.NetworkInformation.Ping.GetNonLoopbackIP () [0x00000] in <filename unknown>:0
at System.Net.NetworkInformation.Ping.SendPrivileged (System.Net.IPAddress address, Int32 timeout, System.Byte[] buffer, System.Net.NetworkInformation.PingOptions options) [0x00000] in <filename unknown>:0
at System.Net.NetworkInformation.Ping.Send (System.Net.IPAddress address, Int32 timeout, System.Byte[] buffer, System.Net.NetworkInformation.PingOptions options) [0x00000] in <filename unknown>:0
at System.Net.NetworkInformation.Ping.Send (System.String hostNameOrAddress, Int32 timeout, System.Byte[] buffer, System.Net.NetworkInformation.PingOptions options) [0x00000] in <filename unknown>:0
at System.Net.NetworkInformation.Ping.Send (System.String hostNameOrAddress, Int32 timeout, System.Byte[] buffer) [0x00000] in <filename unknown>:0
at System.Net.NetworkInformation.Ping.Send (System.String hostNameOrAddress, Int32 timeout) [0x00000] in <filename unknown>:0
at System.Net.NetworkInformation.Ping.Send (System.String hostNameOrAddress) [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.Net.NetworkInformation.Ping:Send (string)
at Internet.Connection.Check () [0x00000] in <filename unknown>:0
at MyGame.Load.Loader.StartLoading () [0x00000] in <filename unknown>:0
at Loader+<Start>c__IteratorA.MoveNext () [0x00000] in <filename unknown>:0
(Filename: Line: -1)
Code:
namespace Internet
{
using System;
using System.Net.NetworkInformation;
using UnityEngine;
public class Connection
{
public static void Check()
{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
if (ping.Send("89.203.249.74").Status != IPStatus.Success)
{
Debug.Log("NO INTERNET CONNECTION");
Application.Quit();
}
}
}
}
So Please help

How to fix a Memory Map Failed Exception for GPIO write on raspberry pi

I'm trying to use the Raspberry Sharp IO library to write to a pin on the Pi. But it gives me an exception on runtime. It had worked before, but doesnt any more. Why is this error being thrown?
using System;
using Raspberry.IO.GeneralPurpose;
using Raspberry.IO.GeneralPurpose.Behaviors;
using System.Threading;
namespace blinky
{
class MainClass
{
public static void Main (string[] args)
{
// Here we create a variable to address a specific pin for output
// There are two different ways of numbering pins--the physical numbering, and the CPU number
// "P1Pinxx" refers to the physical numbering, and ranges from P1Pin01-P1Pin40
var led1 = ConnectorPin.P1Pin03.Output();
// Here we create a connection to the pin we instantiated above
var connection = new GpioConnection(led1);
while(true){
// Toggle() switches the high/low (on/off) status of the pin
connection.Toggle(led1);
Thread.Sleep(250);
}
// connection.Close();
}
}
}
returns this error:
pi#Minion01 ~/blinky1/blinky1/bin/Debug $ mono ./blinky1.exe
Unhandled Exception:
Raspberry.IO.Interop.MemoryMapFailedException: Exception of type 'Raspberry.IO.Interop.MemoryMapFailedException' was thrown.
at Raspberry.IO.Interop.MemoryMap.ThrowOnError[MemoryMapFailedException] (IntPtr result) [0x00000] in <filename unknown>:0
at Raspberry.IO.Interop.MemoryMap.Create (IntPtr address, UInt32 size, MemoryProtection protection, MemoryFlags memoryflags, Int32 fileDescriptor, UInt32 offset) [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnectionDriver..ctor () [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnectionSettings.GetBestDriver (GpioConnectionDriverCapabilities capabilities) [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnectionSettings.get_DefaultDriver () [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnectionSettings..ctor () [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnection..ctor (Raspberry.IO.GeneralPurpose.GpioConnectionSettings settings, IEnumerable`1 pins) [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnection..ctor (Raspberry.IO.GeneralPurpose.PinConfiguration[] pins) [0x00000] in <filename unknown>:0
at blinky.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: Raspberry.IO.Interop.MemoryMapFailedException: Exception of type 'Raspberry.IO.Interop.MemoryMapFailedException' was thrown.
at Raspberry.IO.Interop.MemoryMap.ThrowOnError[MemoryMapFailedException] (IntPtr result) [0x00000] in <filename unknown>:0
at Raspberry.IO.Interop.MemoryMap.Create (IntPtr address, UInt32 size, MemoryProtection protection, MemoryFlags memoryflags, Int32 fileDescriptor, UInt32 offset) [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnectionDriver..ctor () [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnectionSettings.GetBestDriver (GpioConnectionDriverCapabilities capabilities) [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnectionSettings.get_DefaultDriver () [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnectionSettings..ctor () [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnection..ctor (Raspberry.IO.GeneralPurpose.GpioConnectionSettings settings, IEnumerable`1 pins) [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnection..ctor (Raspberry.IO.GeneralPurpose.PinConfiguration[] pins) [0x00000] in <filename unknown>:0
at blinky.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0
How may i fix it?
I had tried to run this through monodevelop on the pi and also using mono filename.exe . Neither worked. But it did when I used sudo ./filename.exe.

Exception thrown when using Mono+Aspose.Words running on ubuntu

I'm using Aspose.Words to convert a doc/docx file to a pdf file on ubuntu. Mono runtime has been installed. My code is below:
Document doc = new Document ("/data/upfile/test.docx");
doc.Save ("/data/upfile/test.pdf", SaveFormat.Pdf);
Somehow, when the second line of the code is executed, an error was thrown. The detail is below:
Unhandled Exception: System.ArgumentOutOfRangeException: startIndex +
length > this.length Parameter name: length at
System.String.Substring (Int32 startIndex, Int32 length) [0x00000] in
:0 at
x6c95d9cf46ff5f25.x0d299f323d241756.x3b67e683e83cab62 (System.String
xe4115acdf4fbfccc, Int32 x961016a387451f05) [0x00000] in :0 at x4adf554d20d941a6.x50e6e6d123fa4a35.x4469479613bec4d8
(Aspose.Words.Node xda5bf54deb817e37) [0x00000] in :0 at x4adf554d20d941a6.x50e6e6d123fa4a35.x7be41bff00df2c71
(Aspose.Words.Node xda5bf54deb817e37) [0x00000] in :0 at x4adf554d20d941a6.x50e6e6d123fa4a35.x699caa1974f95d1a
(Aspose.Words.Node xda5bf54deb817e37) [0x00000] in :0 at x4adf554d20d941a6.x50e6e6d123fa4a35.x21e4bd206d6231f8
(System.Object xa860e35844c20ac7) [0x00000] in :0
at x4adf554d20d941a6.x50e6e6d123fa4a35.xb5ef8a04e65b87e6
(System.String x1f25abf5fb75e795, System.Object xa860e35844c20ac7)
[0x00000] in :0 at
x59d6a4fc5007b7a4.xdeb77ea37ad74c56.xbbf9a1ead81dd3a1 (System.String
x1f25abf5fb75e795, System.Object xa860e35844c20ac7) [0x00000] in
:0 at
x59d6a4fc5007b7a4.x487cdc969fefe3d6.xbbf9a1ead81dd3a1 (System.String
x1f25abf5fb75e795, System.Object xa860e35844c20ac7) [0x00000] in
:0 at
x59d6a4fc5007b7a4.x487cdc969fefe3d6.xf3a1cfbffa44728c () [0x00000] in
:0 at
x59d6a4fc5007b7a4.x487cdc969fefe3d6.x47f176deff0d42e2 () [0x00000] in
:0 at
x59d6a4fc5007b7a4.x487cdc969fefe3d6.x83f07df6a659e05b () [0x00000] in
:0 at
x59d6a4fc5007b7a4.xcde671c53995c411.xd9db07500873ae98
(Aspose.Words.Document x3664041d21d73fdc,
x59d6a4fc5007b7a4.xdeb77ea37ad74c56 xdfde339da46db651) [0x00000] in
:0 at Aspose.Words.Document.UpdatePageLayout ()
[0x00000] in :0 at
Aspose.Words.Document.x410db0f4a89a6ef1 (Boolean x24af0daafcd8fb8c)
[0x00000] in :0 at
Aspose.Words.Document.get_xcde671c53995c411 () [0x00000] in :0 at Aspose.Words.Document.get_PageCount () [0x00000] in
:0 at
xf989f31a236ff98c.x6c74398bceb133f8.xa2e0b7f7da663553
(x28925c9b27b37a46.x8556eed81191af11 x5ac1382edb7bf2c2) [0x00000] in
:0 at Aspose.Words.Document.xf381a641001e6830
(System.IO.Stream xcf18e5243f8d5fd3, System.String xafe2f3653ee64ebc,
Aspose.Words.Saving.SaveOptions xc27f01f21f67608c) [0x00000] in
:0 at Aspose.Words.Document.Save (System.String
fileName, Aspose.Words.Saving.SaveOptions saveOptions) [0x00000] in
:0 at Aspose.Words.Document.Save (System.String
fileName, SaveFormat saveFormat) [0x00000] in :0
at MonoTest.MainClass.Main (System.String[] args) [0x00000] in
:0 [ERROR] FATAL UNHANDLED EXCEPTION:
System.ArgumentOutOfRangeException: startIndex + length > this.length
Parameter name: length at System.String.Substring (Int32 startIndex,
Int32 length) [0x00000] in :0 at
x6c95d9cf46ff5f25.x0d299f323d241756.x3b67e683e83cab62 (System.String
xe4115acdf4fbfccc, Int32 x961016a387451f05) [0x00000] in :0 at x4adf554d20d941a6.x50e6e6d123fa4a35.x4469479613bec4d8
(Aspose.Words.Node xda5bf54deb817e37) [0x00000] in :0 at x4adf554d20d941a6.x50e6e6d123fa4a35.x7be41bff00df2c71
(Aspose.Words.Node xda5bf54deb817e37) [0x00000] in :0 at x4adf554d20d941a6.x50e6e6d123fa4a35.x699caa1974f95d1a
(Aspose.Words.Node xda5bf54deb817e37) [0x00000] in :0 at x4adf554d20d941a6.x50e6e6d123fa4a35.x21e4bd206d6231f8
(System.Object xa860e35844c20ac7) [0x00000] in :0
at x4adf554d20d941a6.x50e6e6d123fa4a35.xb5ef8a04e65b87e6
(System.String x1f25abf5fb75e795, System.Object xa860e35844c20ac7)
[0x00000] in :0 at
x59d6a4fc5007b7a4.xdeb77ea37ad74c56.xbbf9a1ead81dd3a1 (System.String
x1f25abf5fb75e795, System.Object xa860e35844c20ac7) [0x00000] in
:0 at
x59d6a4fc5007b7a4.x487cdc969fefe3d6.xbbf9a1ead81dd3a1 (System.String
x1f25abf5fb75e795, System.Object xa860e35844c20ac7) [0x00000] in
:0 at
x59d6a4fc5007b7a4.x487cdc969fefe3d6.xf3a1cfbffa44728c () [0x00000] in
:0 at
x59d6a4fc5007b7a4.x487cdc969fefe3d6.x47f176deff0d42e2 () [0x00000] in
:0 at
x59d6a4fc5007b7a4.x487cdc969fefe3d6.x83f07df6a659e05b () [0x00000] in
:0 at
x59d6a4fc5007b7a4.xcde671c53995c411.xd9db07500873ae98
(Aspose.Words.Document x3664041d21d73fdc,
x59d6a4fc5007b7a4.xdeb77ea37ad74c56 xdfde339da46db651) [0x00000] in
:0 at Aspose.Words.Document.UpdatePageLayout ()
[0x00000] in :0 at
Aspose.Words.Document.x410db0f4a89a6ef1 (Boolean x24af0daafcd8fb8c)
[0x00000] in :0 at
Aspose.Words.Document.get_xcde671c53995c411 () [0x00000] in :0 at Aspose.Words.Document.get_PageCount () [0x00000] in
:0 at
xf989f31a236ff98c.x6c74398bceb133f8.xa2e0b7f7da663553
(x28925c9b27b37a46.x8556eed81191af11 x5ac1382edb7bf2c2) [0x00000] in
:0 at Aspose.Words.Document.xf381a641001e6830
(System.IO.Stream xcf18e5243f8d5fd3, System.String xafe2f3653ee64ebc,
Aspose.Words.Saving.SaveOptions xc27f01f21f67608c) [0x00000] in
:0 at Aspose.Words.Document.Save (System.String
fileName, Aspose.Words.Saving.SaveOptions saveOptions) [0x00000] in
:0 at Aspose.Words.Document.Save (System.String
fileName, SaveFormat saveFormat) [0x00000] in :0
at MonoTest.MainClass.Main (System.String[] args) [0x00000] in
:0
Any advice is appreciated. Thanks.
Often inner exceptions is a very common problem for converting words documents in PDF via Aspose.
It can be due to some reasones like:
2013 MS Word office.
Some complex elements in word
Old version of Aspose.Words.dll.
Solutions:
Try to update Aspose.Words.dll to new version. If it's still not working - write to aspose forum, and this error will be fixed in new release.
Check word document itself. may be you can find something thac call this inner exception, than try to remove it and check again. If this is acceptable of course.

ObjectDisposedException when selecting DataGridView cells?

I think I have found a bug in mono, but I have not isolated it completely, I need help to isolate my problem.
To reproduce this error
run csharp HelloWorld.cs
click any cell
modify the contents of that cell.
click on any other cell.
NOTE: I think my question is related to this question but is closer to isolating the problem.
Note: I have tried this on mono 3.0.4 and mono 3.0.1 and mono 2.10.x on linux
NOTE 2: I have also tried compiling this into a .exe and it still has the same errors
It will produce this call trace message:
System.ObjectDisposedException: The object was used after being disposed.
at System.Windows.Forms.Control.CreateHandle () [0x00000] in <filename unknown>:0
at System.Windows.Forms.TextBoxBase.CreateHandle () [0x00000] in <filename unknown>:0
at System.Windows.Forms.Control.CreateControl () [0x00000] in <filename unknown>:0
at System.Windows.Forms.Control.SetVisibleCore (Boolean value) [0x00000] in <filename unknown>:0
at System.Windows.Forms.Control.set_Visible (Boolean value) [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.Windows.Forms.Control:set_Visible (bool)
at System.Windows.Forms.DataGridView.BeginEdit (Boolean selectAll) [0x00000] in <filename unknown>:0
at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore (Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick) [0x00000] in <filename unknown>:0
at System.Windows.Forms.DataGridView.OnMouseDown (System.Windows.Forms.MouseEventArgs e) [0x00000] in <filename unknown>:0
at System.Windows.Forms.Control.WmLButtonDblClick (System.Windows.Forms.Message& m) [0x00000] in <filename unknown>:0
at System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message& m) [0x00000] in <filename unknown>:0
at System.Windows.Forms.DataGridView.WndProc (System.Windows.Forms.Message& m) [0x00000] in <filename unknown>:0
at System.Windows.Forms.Control+ControlWindowTarget.OnMessage (System.Windows.Forms.Message& m) [0x00000] in <filename unknown>:0
at System.Windows.Forms.Control+ControlNativeWindow.WndProc (System.Windows.Forms.Message& m) [0x00000] in <filename unknown>:0
at System.Windows.Forms.NativeWindow.WndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam) [0x00000] in <filename unknown>:0
System.ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count.
Parameter name: index
0
at System.Collections.ArrayList.ThrowNewArgumentOutOfRangeException (System.String name, System.Object actual, System.String message) [0x00000] in <filename unknown>:0
at System.Collections.ArrayList.get_Item (Int32 index) [0x00000] in <filename unknown>:0
at System.Windows.Forms.DataGridViewRowCollection.SharedRow (Int32 rowIndex) [0x00000] in <filename unknown>:0
at System.Windows.Forms.DataGridView.GetRowInternal (Int32 rowIndex) [0x00000] in <filename unknown>:0
at System.Windows.Forms.DataGridView.GetCellInternal (Int32 colIndex, Int32 rowIndex) [0x00000] in <filename unknown>:0
at System.Windows.Forms.DataGridView.OnCellLeave (System.Windows.Forms.DataGridViewCellEventArgs e) [0x00000] in <filename unknown>:0
at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore (Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick) [0x00000] in <filename unknown>:0
at System.Windows.Forms.DataGridView.MoveCurrentCell (Int32 x, Int32 y, Boolean select, Boolean isControl, Boolean isShift, Boolean scroll) [0x00000] in <filename unknown>:0
at System.Windows.Forms.DataGridView.OnColumnCollectionChanged (System.Object sender, System.ComponentModel.CollectionChangeEventArgs e) [0x00000] in <filename unknown>:0
at System.Windows.Forms.DataGridViewColumnCollection.OnCollectionChanged (System.ComponentModel.CollectionChangeEventArgs e) [0x00000] in <filename unknown>:0
at System.Windows.Forms.DataGridViewColumnCollection.Clear () [0x00000] in <filename unknown>:0
at System.Windows.Forms.DataGridView.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0
at System.ComponentModel.Component.Dispose () [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.ComponentModel.Component:Dispose ()
at System.Windows.Forms.Control.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0
at System.Windows.Forms.ContainerControl.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0
at System.Windows.Forms.Form.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0
at System.ComponentModel.Component.Dispose () [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.ComponentModel.Component:Dispose ()
at System.Windows.Forms.Form.WmClose (System.Windows.Forms.Message& m) [0x00000] in <filename unknown>:0
at System.Windows.Forms.Form.WndProc (System.Windows.Forms.Message& m) [0x00000] in <filename unknown>:0
at System.Windows.Forms.Control+ControlWindowTarget.OnMessage (System.Windows.Forms.Message& m) [0x00000] in <filename unknown>:0
at System.Windows.Forms.Control+ControlNativeWindow.WndProc (System.Windows.Forms.Message& m) [0x00000] in <filename unknown>:0
at System.Windows.Forms.NativeWindow.WndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam) [0x00000] in <filename unknown>:0
contents of HelloWorld.cs:
using System.Data;
using System.Drawing;
using System.Windows.Forms;
public class HelloWorld : Form {
public HelloWorld () {
Button quit = new Button ();
quit.Text = "quit";
quit.Click += new EventHandler (OnClickQuit);
quit.Location = new Point(500, 400);
DataGridView dgv = new DataGridView();
dgv.AutoSize = true;
DataTable table1 = new DataTable("patients");
table1.Columns.Add("name");
table1.Columns.Add("id");
table1.Rows.Add("sam", 1);
table1.Rows.Add("mark", 2);
DataSet ds = new DataSet("office");
ds.Tables.Add(table1);
dgv.DataSource = ds;
dgv.DataMember = ds.Tables[0].TableName;
Controls.AddRange(new Control[] { dgv, quit });
}
void OnClickQuit(object sender, EventArgs e) {
Close();
}
}
Application.Run (new HelloWorld ());

Processing Linq XML string

I need to parse XML string with Linq, and I came up with the following code.
using System;
using System.Linq;
using System.Xml.Linq;
class LinqXml
{
public void Parse(string input)
{
XDocument xdoc = XDocument.Load(input);
var lang = from d in xdoc.Elements("PipeUnit").Elements("Step").Elements("Pipelist").Elements("NamedPipe").Elements("NameOfPipe") select d;
Console.WriteLine(lang.First().Value);
foreach (var item in lang)
{
Console.WriteLine(item.Value);
}
}
static void Main()
{
string tempString = #"
<PipeUnit>
<Step>
<Pipelist>
<NamedPipe>
<NameOfPipe>Name</NameOfPipe>
<PipeData>Data</PipeData>
</NamedPipe>
</Pipelist>
</Step>
</PipeUnit>
";
var linqXml = new LinqXml();
linqXml.Parse(tempString);
}
}
When compile this code with Mono - dmcs linqxml.cs /r:System.Xml.Linq.dll, and tried to run, I got the following errors.
Unhandled Exception: System.IO.DirectoryNotFoundException: Could not find a part of the path "/Users/smcho/Desktop/csharp/
<PipeUnit>
<Step>
<Pipelist>
<NamedPipe>
<NameOfPipe>Name</NameOfPipe>
<PipeData>Data</PipeData>
</NamedPipe>
</Pipelist>
</Step>
<PipeUnit>".
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
at System.Xml.XmlUrlResolver.GetEntity (System.Uri absoluteUri, System.String role, System.Type ofObjectToReturn) [0x00000] in <filename unknown>:0
at Mono.Xml2.XmlTextReader.GetStreamFromUrl (System.String url, System.String& absoluteUriString) [0x00000] in <filename unknown>:0
at Mono.Xml2.XmlTextReader..ctor (Boolean dummy, System.Xml.XmlResolver resolver, System.String url, XmlNodeType fragType, System.Xml.XmlParserContext context) [0x00000] in <filename unknown>:0
at System.Xml.XmlTextReader..ctor (Boolean dummy, System.Xml.XmlResolver resolver, System.String url, XmlNodeType fragType, System.Xml.XmlParserContext context) [0x00000] in <filename unknown>:0
at System.Xml.XmlReader.Create (System.String url, System.Xml.XmlReaderSettings settings, System.Xml.XmlParserContext context) [0x00000] in <filename unknown>:0
at System.Xml.XmlReader.Create (System.String url, System.Xml.XmlReaderSettings settings) [0x00000] in <filename unknown>:0
at System.Xml.Linq.XDocument.Load (System.String uri, LoadOptions options) [0x00000] in <filename unknown>:0
at System.Xml.Linq.XDocument.Load (System.String uri) [0x00000] in <filename unknown>:0
at LinqXml.Parse (System.String input) [0x00000] in <filename unknown>:0
at LinqXml.Main () [0x00000] in <filename unknown>:0
What might be wrong?
Replace this:
XDocument xdoc = XDocument.Load(input);
with:
XDocument xdoc = XDocument.Parse(input);
you are passing XML, not a file name.
in your Parse method, you want
XDocument.Parse(input)
Replace
XDocument xdoc = XDocument.Load(input);
with
XDocument xdoc = XDocument.Parse(input);

Categories