|
Well that link I put in for the How to read from an XML file shows you how to use it, but without knowing about the document, I can't really help with null values. I mean
XMLDocument doc = new XMLDocument();
doc.LoadXML(filename);
foreach(XmlNode node in doc.DocumentElement.ChildNodes){
string text = node.InnerText;
}
its pretty straight forward, but again if you wrap it in a try/catch and read what the catch says I feel confident the solution will present itself.
|
|
|
|
|
this is working really well and displays the results in a message box. The only issue is that when the message boxes are done being displayed the application seems to freeze up and then finally errors out. i think it is because the loop is still running.
One question... is there a way to change the code i have below to include the name= tag in the filter? If i can do that i might not need the loop and can call the nodes directly. example...;
xmlvalue = node.SelectSingleNode("/setting name="AppXml"/value").InnerText;
private void LoadXMLData_Click(object sender, EventArgs e)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load("C:\\user.config");
XmlNodeList nodes = xdoc.DocumentElement.SelectNodes("/configuration/userSettings/nextnode/setting");
string xmlvalue = "";
foreach (XmlNode node in nodes)
{
if (xmlvalue != null)
{
xmlvalue = node.SelectSingleNode("value").InnerText;
string xmlvalue2 = System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(xmlvalue));
MessageBox.Show(xmlvalue2);
}
}
}
modified 12-Feb-18 19:05pm.
|
|
|
|
|
Sample XML
<configuration>
<userSettings>
<AppCentral.App.Service.Core.Properties.Settings>
<setting name="AppXml" serializeAs="String">
<value>PD94bWwgdmVyc2lvbj0iMS4wIj8+DQo8QXBwTW9kZWwgeG1sbnM6eHNpPSJodHRwOi8vd3d3LnczLm9yZy8yMDAxL1hNTFNjaGVtYS1pbnN0YW5jZSIgeG1sbnM6eHNkPSJodHRwOi8vd3d3LnczLm9yZy8yMDAxL1hNTFNjaGVtYSI+DQogIDxDR1VJRD4xMjM0NTY3ODk8L0NHVUlEPg0KICA8Q0lEPjEyMzwvQ0lEPg0KICA8Q051bWJlcj4xMjM0PC9DTnVtYmVyPg0KICA8Q05hbWU+VEVTVDwvQ05hbWU+DQogIDxIYXNDSEluc3RhbGw+ZmFsc2U8L0hhc0NISW5zdGFsbD4NCiAgPFZlbmRvcklEPjA8L1ZlbmRvcklEPg0KPC9BcHBNb2RlbD4=</value>
</setting>
<setting name="TXml" serializeAs="String">
<value>PD94bWwgdmVyc2lvbj0iMS4wIj8+DQo8QXBwVE1vZGVsIHhtbG5zOnhzaT0iaHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxTY2hlbWEtaW5zdGFuY2UiIHhtbG5zOnhzZD0iaHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxTY2hlbWEiPg0KICA8Q0dVSUQgeHNpOm5pbD0idHJ1ZSIgLz4NCiAgPENJRD4xMjM8L0NJRD4NCiAgPElzTWFuYWdlZFRBbGxvd2VkPmZhbHNlPC9Jc01hbmFnZWRUQWxsb3dlZD4NCiAgPElzT25lVGltZVRFbmFibGVkPmZhbHNlPC9Jc09uZVRpbWVURW5hYmxlZD4NCiAgPE9uZVRpbWVUQ2hhcmdlPjIuMDAwMDwvT25lVGltZVRDaGFyZ2U+DQogIDxXYWl0aW5nUGVyaW9kSG91cnM+MzwvV2FpdGluZ1BlcmlvZEhvdXJzPg0KICA8SXNUVGF4YWJsZT5mYWxzZTwvSXNUVGF4YWJsZT4NCiAgPFRUYXhSYXRlPjQuMDAwMDwvVFRheFJhdGU+DQo8L0FwcFRNb2RlbD4=</value>
</setting>
<setting name="DXml" serializeAs="String">
<value>PD94bWwgdmVyc2lvbj0iMS4wIj8+DQo8QXJyYXlPZkRNb2RlbCB4bWxuczp4c2k9Imh0dHA6Ly93d3cudzMub3JnLzIwMDEvWE1MU2NoZW1hLWluc3RhbmNlIiB4bWxuczp4c2Q9Imh0dHA6Ly93d3cudzMub3JnLzIwMDEvWE1MU2NoZW1hIj4NCiAgPERNb2RlbD4NCiAgICA8Q0RJRD4xPC9DRElEPg0KICAgIDxOYW1lPkQxPC9OYW1lPg0KICAgIDxUeXBlPjE8L1R5cGU+DQogICAgPENQb3J0PjE8L0NQb3J0Pg0KICAgIDxSSW5kZXg+MTwvUkluZGV4Pg0KICAgIDxJc0FjdGl2ZT5mYWxzZTwvSXNBY3RpdmU+DQogICAgPE9wYWNpdHk+MDwvT3BhY2l0eT4NCiAgICA8U2NoZWR1bGVzIC8+DQogIDwvRE1vZGVsPg0KICA8RE1vZGVsPg0KICAgIDxDRElEPjI8L0NESUQ+DQogICAgPE5hbWU+VDE8L05hbWU+DQogICAgPFR5cGU+ODwvVHlwZT4NCiAgICA8Q1BvcnQ+NTwvQ1BvcnQ+DQogICAgPFJJbmRleD4xPC9SSW5kZXg+DQogICAgPElzQWN0aXZlPmZhbHNlPC9Jc0FjdGl2ZT4NCiAgICA8T3BhY2l0eT4wPC9PcGFjaXR5Pg0KICAgIDxTY2hlZHVsZXMgLz4NCiAgPC9ETW9kZWw+DQogIDxETW9kZWw+DQogICAgPENESUQ+MzwvQ0RJRD4NCiAgICA8TmFtZT5EMjwvTmFtZT4NCiAgICA8VHlwZT4xPC9UeXBlPg0KICAgIDxDUG9ydD4zPC9DUG9ydD4NCiAgICA8UkluZGV4PjE8L1JJbmRleD4NCiAgICA8SXNBY3RpdmU+ZmFsc2U8L0lzQWN0aXZlPg0KICAgIDxPcGFjaXR5PjA8L09wYWNpdHk+DQogICAgPFNjaGVkdWxlcz4NCiAgICAgIDxEU2NoZWR1bGU+DQogICAgICAgIDxDRFNjaGVkdWxlSUQ+MDwvQ0RTY2hlZHVsZUlEPg0KICAgICAgICA8SG91clN0YXJ0PjY8L0hvdXJTdGFydD4NCiAgICAgICAgPEhvdXJFbmQ+NzwvSG91ckVuZD4NCiAgICAgICAgPElzQXZhaWxhYmxlPnRydWU8L0lzQXZhaWxhYmxlPg0KICAgICAgPC9EU2NoZWR1bGU+DQogICAgPC9TY2hlZHVsZXM+DQogIDwvRE1vZGVsPg0KPC9BcnJheU9mRE1vZGVsPg==</value>
</setting>
<setting name="CurrentVersion" serializeAs="String">
<value>1</value>
</setting>
</AppCentral.App.Service.Core.Properties.Settings>
</userSettings>
</configuration>
|
|
|
|
|
sorry, im not looking for you to write it for me. This code is for my own person application i am developing as i learn on my own. I am trying to self teach myself C#. I am not working on this for a school or business project.
I am reading over the class and trying to figure out how to include this in my method. Here is what i have so far. Can you at least show me where i might be going wrong? i used the link you sent me as a template.
<pre lang="c#"> public void XMLStrings(string filename)
{
// Create an instance of the XmlSerializer specifying type and namespace.
XmlSerializer serializer = new
XmlSerializer(typeof(XMLClass));
// A FileStream is needed to read the XML document.
FileStream fs = new FileStream(filename, FileMode.Open);
XmlReader reader = XmlReader.Create(fs);
// Declare an object variable of the type to be deserialized.
XMLClass i;
// Use the Deserialize method to restore the object's state.
i = (XMLClass)serializer.Deserialize(reader);
fs.Close();
// Write out the properties of the object.
textBox1.AppendText(
i.something1 + "\t" +
i.something2 + "\t" +
i.something3);
}</pre>
|
|
|
|
|
Nobody seems to have mentioned this, but Base64 is NOT an encryption scheme. It's one of a bunch of encoding schemes originally designed to enable the transport of binary data over links that doesn't support it.
Look familiar?
<?xml version="1.0"?>
<AppModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CGUID>123456789</CGUID>
<CID>123</CID>
<CNumber>1234</CNumber>
<CName>TEST</CName>
<HasCHInstall>false</HasCHInstall>
<VendorID>0</VendorID>
</AppModel>
Why are you embedding XML inside an XML file? This seems a bit overly complicated.
|
|
|
|
|
Hi all,
I am trying to develop a C# gui,
having 1 base clase,
a derived class with virtual methods (Parent)
and a further derived class(Child) implementing these methods.
and therefore update a richtextbox on the baseClass from the childClass on button click.
I have created an instance of the childclass in the baseClass,
and have a variable of the baseclass in the childClass like I've seen in some threads online and passed this variable into the constructor of the childClass.
When running the code, it compiles but once the button is clicked, the created variable remains null??
Struggling to figure this out, but I am sure it is some small reason :/
Below is my code I am working with..
namespace Polymorphism
{
public partial class Base : Window
{
private static readonly Base win;
public static Parent sendr = new Child(win);
public Base()
{
InitializeComponent();
}
public void Button_Click(object sender, RoutedEventArgs e)
{
string text = "text";
RtbWrite(text);
}
public void RtbWrite(string text)
{
sendr.Send(text);
}
}
public class Parent : Base
{
public virtual void Send(string text){ }
}
class Child : Parent
{
public Base win;
public Child(Base win)
{
this.win = win;
}
public override void Send(string text)
{
win.Rtb.AppendText("Sent");
}
}
}
|
|
|
|
|
Base contains an object of Parent , but Parent is a child of Base . Base contains a static Base object (win ), but Child , which is a child of Parent , contains a non-static Base object also called win . Sorry, but I am totally confused.
|
|
|
|
|
"Sub classing" was a "bad habit" that I had until I decided it was more trouble in the long run.
VB's "component based approach" is actually better in some cases than OOP(s); as in the UI.
I think you should just say "what" you want to accomplish; and you may get some good suggestions on how to accomplish it with less indirection.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Thanks for the replies Richard and Gerry!
I am creating a GUI to connect to a diagnostics tool.
I need to connect to the tool via ethernet and serial connections therefore need to have a class for each connection method.
I was told to have a base class which calls virtual methods in a 'connection' class that are overridden in either the ethernet/serial connection.
I am able to connect to the tool, but when writing information back to the GUI e.g. 'Connected to the tool' etc I am unable to do this..
Thanks in advance for any advice!
G
|
|
|
|
|
The preferred approach is an "interface" ... "one codes to the interface".
You create "one" window, and "inject" the "port" you want; say, via the form's / window constructor; then reference the injected device via its common interface. e.g. (semi pseudo):
interface IPort{
// Properties and methods common to device/com api's
Open();
string Receive();
// etc.
}
// class "implements" interface.
public class MySerial : IPort {
.. Open(){ // custom serial code }
... }
class main {
// Program "instantiates" an obj with said interface.
IPort ActivePort = new MySerial();
...
// Program uses obj via interface.
ActivePort.Open();
etc.
}
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Hi Gerry,
Thank you very much for that detailed answer.
I will try implementing an interface as you suggest and see if I can get it to work!
Thanks again,
will report back my progress.
G
|
|
|
|
|
You're welcome!
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Pls provide me code for above subject...
aaa
|
|
|
|
|
How many times are you going to ask this same question - that's six so far?
The answer isn't going to change:
It doesn't quite work like that.
We do not do your work for you.
If you want someone to write your code, you have to pay - I suggest you go to Freelancer.com and ask there.
But be aware: you get what you pay for. Pay peanuts, get monkeys.
The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements." BusinessDictionary.com[^]
That's not the same thing as "have a quick google and give up if I can't find exactly the right code".
So either pay someone to do it, or learn how to write it yourself. We aren't here to do it for you.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Password to "what"?
Since you don't know that, how do you know it needs to be in C#?
You don't.
Even your emoji is irrelevant.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Wow this conversation is becoming interesting 😂!
|
|
|
|
|
There's no "conversation"; there's nothing to "converse" over.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
By the way, you guys are coder or moderator?
|
|
|
|
|
Vigilantes
and Hell's comin' with us
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
If this is for a MVC app, there's plenty of info if you google this phrase: "mvc forgotten password".
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
I am trying to create a function in the GUI where I Logg the values from a textbox and then allow the user to save these values as a .csv file anywhere on the computer.
I have managed to do that, but when I open the files I only see one item from my textbox written in the text file, Why is that ?
private void Logging()
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using (Stream s = File.Open(saveFileDialog1.FileName, FileMode.CreateNew))
using (StreamWriter sw = new StreamWriter(s))
{
sw.WriteLine(textSampling.Text);
}
}
|
|
|
|
|
You are using File.Open(saveFileDialog1.FileName, FileMode.CreateNew) every time, so the file will only ever contain that last item logged. You need to use FileMode.Append as described at FileMode Enumeration (System.IO)[^].
|
|
|
|
|
Hi, how am I suppose to implement it with my code.
i have code that is doing almost everything I want, only not printing the contents from the text.Box. The strange thing is, when I use a RichTextBox then everything gets printed into the .csv file I am trying to create.
Were you thinking about something like this?
private void Logging()
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using (Stream s = File.Open(saveFileDialog1.FileName, FileMode.CreateNew | FileMode.Append))
using (StreamWriter sw = new StreamWriter(s))
{
sw.WriteLine(textSampling.Text);
}
}
|
|
|
|
|
I already explained, that if you use FileMode.CreateNew you will be creating a new file every time you try to write a log entry. You only need to use that once, at the beginning of a session perhaps*, and use FileMode.Append for every subsequent call.
* You need to use some sort of file name cycling in your programs so you do not overwrite the previously created files. A common method of doing this is to create files with the date as part of the filename.
|
|
|
|
|
For "simple" logging, all you need is:
1) File.AppendAllText, or
2) File.AppendAllLines
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|