Error in code: Loading of XML document - c#

this is in reference to a question asked earlier. Here is the complete code and it's giving me an error:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Xml;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
namespace LoadXMLtreeDisplay
{
public partial class TreeDisplay : Form
{
public string Filename
{
get { return filename; }
}
protected string filename;
public TreeDisplay()
{
InitializeComponent();
this.Text = "Tree View of XML File";//Form Title.
}
private void treeDocLoadMethod(string nameofFile)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(nameofFile);
this.treeView1.Nodes.Clear();
this.treeView1.Nodes.Add(new TreeNode(xdoc.DocumentElement.Name));
TreeNode tNodeObj = new TreeNode();
tNodeObj = this.treeView1.Nodes[0];
XmlNode xNode = xdoc.DocumentElement;
AddingNodesToTree(tNodeObj, xNode);
treeView1.Nodes[0].Expand();
treeView1.CollapseAll();
}//treeDocLoadMethod
private void AddingNodesToTree(refXmlNode xnode,TreeNode tnode)
{
TreeNode subNode = tnode.Add(xnode.Name);
subNode.Tag = xnode;
foreach (XmlNode subElement in xnode.ChildNodes)
{
AddingNodesToTree(ref subElement, ref subNode);
}
}
private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{ listBox1.Items.Clear();
TreeNode currentNode = e.Node;
XmlElement currentElement = (XmlElement)currentNode.Tag;
XmlAttributeCollection attCol = currentElement.Attributes;
foreach (XmlAttribute xmlatt in attCol)
{
listBox1.Items.Show(xmlatt);
}
}
//Rest of the code is for winform display(various buttons and boxes)
}
}
After compiling this I am getting the error:
Expected class, delegate, enum,
interface, or struct Type or
namespace definition, or end-of-file
expected
Please could you tell me what's the possible source of error and how to rectify it? I am a newbie in C#

There's a space missing in the first line; should be "ref XmlNode" instead of "refXmlNode"
Edit: not the first line (any more); but the line that says:
private void AddingNodesToTree(refXmlNode xnode,TreeNode tnode)

Related

Follow relative link in FlowDocument

I am trying to use a WPF application in C# to display a RTF document which contains some relative links. To process the links in the document I found the following code online. I have verified that a link to the URL www.google.com works.
Now the problem is the RTF contains a few links to other pages, such as a link to the bookmark "Pag2". How do i get those to work?
//Code to process the links:
using System.Windows.Documents;
using System.Collections.Generic;
using System.Windows;
using System.Linq;
using System.Diagnostics;
#region Activate Hyperlinks in the Rich Text box
static class Hyperlinks
{
//http://stackoverflow.com/questions/5465667/handle-all-hyperlinks-mouseenter-event-in-a-loaded-loose-flowdocument
public static void SubscribeToAllHyperlinks(FlowDocument flowDocument)
{
var hyperlinks = GetVisuals(flowDocument).OfType<Hyperlink>();
foreach (var link in hyperlinks)
link.RequestNavigate += new System.Windows.Navigation.RequestNavigateEventHandler(link_RequestNavigate);
}
public static IEnumerable<DependencyObject> GetVisuals(DependencyObject root)
{
foreach (var child in LogicalTreeHelper.GetChildren(root).OfType<DependencyObject>())
{
yield return child;
foreach (var descendants in GetVisuals(child))
yield return descendants;
}
}
public static void link_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
//http://stackoverflow.com/questions/2288999/how-can-i-get-a-flowdocument-hyperlink-to-launch-browser-and-go-to-url-in-a-wpf
if (e.Uri.IsAbsoluteUri)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
}
else
{
// code to navigate to relative uri such as #Page2??
}
e.Handled = true;
}
}
#endregion Activate Hyperlinks in the Rich Text box
//Calling code:
class FlowDocTest_VM
{
public FlowDocTest_VM()
{
string filename = "D:\\Manual.rtf";
fdContent = new FlowDocument();
FileStream fsHelpfile = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
TextRange trContent = new TextRange(fdContent.ContentStart, fdContent.ContentEnd);
trContent.Load(fsHelpfile, DataFormats.Rtf);
Hyperlinks.SubscribeToAllHyperlinks(fdContent);
}
public FlowDocument Document
{
get
{
return fdContent;
}
set
{
fdContent = value;
}
}
private FlowDocument fdContent;
}

Loading XmlDocument into Webform control

I have created a WCF service which sends an sms by using the supplied values. This then return XML in a string variable. I have managed to get the string loaded into an XmlDocument but then when I try and load it onto a Webform control I get an error because the XmlDocument is in memory and does not have a physical path. How can I get this loaded into my webform control(WB_XMLOut). Please see my code below what I have tried.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LeadProcessTest.LeadProcessor;
using System.Xml;
namespace LeadProcessTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void BT_Process_Click(object sender, EventArgs e)
{
LeadProcessorClient LP = new LeadProcessorClient();
string UName = Convert.ToBase64String(Encoding.UTF8.GetBytes(TB_UName.Text));
string PWord = Convert.ToBase64String(Encoding.UTF8.GetBytes(TB_Pass.Text));
string XMLIn = Convert.ToBase64String(Encoding.UTF8.GetBytes(TB_XMLin.Text));
LP.Open();
string Result = LP.Lead_Processing(UName, PWord, XMLIn);
LP.Close();
XmlDocument XML = new XmlDocument();
XML.LoadXml(Result);
WB_XMLOut.Url = new Uri(XML);
}
}
}

Using CodeDOM to source code and class file togther

i have created a form for a codeDOM compiler and it can compile my code if its in a single text file but i want to be able to compile the source code in the text file and a class thats in a text file so they can work together.
I am unsure how to code codeDOM to add my class file as a resource for the main source to be compiled
Heres what i have so far
codeDOM Compiler
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.CodeDom.Compiler;
using Microsoft.CSharp;
namespace CodeDOMSourceTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnCompile_Click(object sender, EventArgs e)
{
CompilerParameters Params = new CompilerParameters();
Params.GenerateExecutable = true;
Params.ReferencedAssemblies.Add("System.dll");
Params.ReferencedAssemblies.Add("TextFile1.txt");
Params.OutputAssembly = "output.exe";
string Source = Properties.Resources.CodeDOMSource;
Source = Source.Replace("[TEXT]", txtReplace.Text);
CompilerResults Results = new CSharpCodeProvider().CompileAssemblyFromSource(Params, Source);
if (Results.Errors.Count > 0)
{
foreach (CompilerError err in Results.Errors)
Console.WriteLine(err.ToString());
}
else Console.WriteLine("Compiled just fine!");
}
}
}
Source file
namespace testingCodeDOM
{
class Program
{
static void Main()
{
System.Console.WriteLine("[TEXT]");
Console.WriteLine(Testing.textwork());
System.Console.Read();
}
}
}
ClassFile
namespace testingCodeDOM
{
class Testing
{
public static string textwork()
{
string hello = "calss worked";
return hello;
}enter code here
}
}
Any ideas how to do this because i have googled it an am getting no were or at least nothing i understand
on a side not this works with with just the source but am trying to adapt it to use class files aswell
The CompileAssemblyFromSource() method can take any number of source code strings (since it's a params method). So, you can call it something like:
CompileAssemblyFromSource(Params, Source, ClassFile)
Where ClassFile is a string that contains the text of the second file.

Call a function from another class in C#

I am dealing with a new project. I have a class to draw Rectangles to windows form. I want to embed this class to another class. Code's below;
Main Code will call the shape code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using sekilciz_uygulama;
namespace xml_test_v1
{
class Program
{
static void Main(string[] args)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load("c:\\sw_xml_test_4.xml");
int rad=0;
string giris_text = Console.ReadLine().ToString();
Console.WriteLine(giris_text);
foreach(XmlNode node in xDoc.SelectNodes("network/switch"))
{
string ip_adress = node.SelectSingleNode("ip_adress").InnerText.ToString();
Console.WriteLine(ip_adress);
if (ip_adress.Contains(giris_text))
{
// call for shape code!!!
}
}}}}
Code for Creating Shapes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace sekilciz_uygulama
{
public class Sekilciz
{
public Rectangle[] skare;
private SolidBrush firca;
private int x,y, genislik, yukseklik;
public Sekilciz()
{
skare = new Rectangle[5];
firca = new SolidBrush(Color.Blue);
x = 500;
y = 200;
genislik= 100;
yukseklik =100;
for(int i=0; i< skare.Length;i++)
{
skare[i] = new Rectangle(x,y,genislik,yukseklik);
x-=150;
}
}
public void kareciz(Graphics duzlem)
{
foreach(Rectangle rec in skare)
{
duzlem.FillRectangle(firca,rec);
}
}
}
}
var sekilciz = new Sekilciz();
sekilciz.kareciz(null);
You need to pass a parameter to your method kareciz
// "this" is your windows form, or control like button
var myGraphic = this.CreateGraphics();
var sekilciz = new Sekilciz(myGraphic);
sekilciz.kareciz();
But you have too many processing going on in your constructor. It is better to move that code in some other method in the same class.
Look here to complete sample on drawing on windows form:
The Basics of Drawing Graphics onto Windows Forms

Combobox Examples

I am getting the following error for foreach
Foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'
I am making any syntax error
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;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Namepopu_SelectedIndexChanged(object sender, EventArgs e)
{
// this.textBox1.Text = Namepopu.Text;
// this.textBox1.Text = " ";
foreach (int i in Namepopu.SelectedItem)
this.textBox1.Text += Namepopu.Text[i];
{
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Perhaps you meant to do this?
for (int i = 0; i < Namepopu.Items.Count; ++i)
{
this.textBox1.Text += Namepopu.Items[i].ToString();
}
What's the contents of Namepopu.SelectedItem ? An array ? A generic list ? Cast it to it's original type first, then iterate over it in your foreach.
For example:
List<int> myValues = (List<int>)Namepopu.SelectedItem;
foreach (int i in myValues)
{
...
}
or
int[] myValues = (int[])Namepopu.SelectedItem;
foreach (int i in myValues)
{
...
}
textBox1.Text = string.Empty;
foreach (var item in Namepopu.SelectedItems)
textBox1.Text += item.ToString();
The problem is that you are trying to get the enumerator for something that does not implement the IEnumerator interface. For a listing of the collection types you can iterate using a for each this MSDN article is useful (http://msdn.microsoft.com/en-us/library/dscyy5s0.aspx).

Categories