|
Hello all,
I am working on on functionality in which i want to write a c# function which will read XML file from folder.
And it will write .txt file from it and will put it in other folder.
can some one help me to do this, when i am reading XML file line by line i should able to write any error or log in log file, if some thing is wrong it should rollback all file else generate .txt file.
Needs to implement it using any design pattern.
|
|
|
|
|
Technically, an XML file is already a text file. All you need to do is change the file extension.
Now, if you actually want to transform the XML in some way, then you're going to need to either write code to do it, or write an XSLT transform[^] to do it.
But since you've provided precisely zero details about the input and expected output, nobody can possibly help you.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
There are several tools for reading an arbitrary XML file into a DOM tree. Use that, rather than reading XML line by line.
This is a plain tree that can easily be traversed like any other tree, usually with a recursive procedure. Depending on the data in the tree and your requirements for the result, you do the traversal depth first or breath first. When you find some value that appears to be text of the kind you are after, you write that to your text output file and continue the search.
Maybe there XML structure contains non-text information that you should nevertheless interpret, and produce the appropriate output. E.g. if a node is flagged as a h1, you will probably want to add an extra blank line above the heading text.
Religious freedom is the freedom to say that two plus two make five.
|
|
|
|
|
Member 12116052 wrote: XML ... put it in other folder.
1. Read and process the XML
2. Determine the output.
3. Open the output file
4. Write 2 to the file from 3.
Member 12116052 wrote: when i am reading XML file line by line i s
Nope. Bad idea.
You do not read XML 'line by line'. Rather you parse it. And then extract values from what you parsed.
If and ONLY if the source of the XML can be guaranteed to be an automated single source, then you can roll the dice and attempt to write your own parser (not an XML parser) which can parse individual lines. Thus skipping the XML learning curve.
And also making your code sure to break if the format of the XML changes.
|
|
|
|
|
To add to what the others say, you need to sit down and work out exactly what you expect to generate form your XML data.
The reason being that XML is a text-based hierarchically structured data format, and it can be all in a single line: newlines (or any other whitespace) are not required by the XML specification, and "individual items" in XML can span multiple lines of text without any change in the data it encapsulates or an entire array of objects can occupy a single line of text - that's problem number one!
The second problem is that the data contained in the XML itself doesn't "lend itself" to a flat file output - which is what an unformatted text file contains.
If you look at a "basic XML" file like this example: W3Schools CD Catalog[^] you can see that what it contains isn't readily useful as a text file unless you actually convert that to a formatted file of some form. Just stripp[ing out the XML stuff doesN't give you any useful data:
Empire BurlesqueBob DylanUSAColumbia10.901985Hide your heartBonnie TylerUKCBS
Records9.901988Greatest HitsDolly PartonUSARCA9.901982Still got the bluesGary
MooreUKVirgin records10.201990ErosEros RamazzottiEUBMG9.901997One night onlyBee
GeesUKPolydor10.901998Sylvias MotherDr.HookUKCBS8.101973Maggie MayRod
StewartUKPickwick8.501990RomanzaAndrea BocelliEUPolydor10.801996When a man loves a
womanPercy SledgeUSAAtlantic8.701987Black angelSavage RoseEUMega10.9019951999 Grammy
NomineesManyUSAGrammy10.201999For the good timesKenny RogersUKMucik Master8.701995Big
Willie styleWill SmithUSAColumbia9.901997Tupelo HoneyVan
MorrisonUKPolydor8.201971SoulsvilleJorn HoelNorwayWEA7.901996The very best
ofCatStevensUKIsland8.901990StopSam BrownUKA and M8.901988Bridge of
SpiesT'PauUKSiren7.901987Private DancerTina TurnerUKCapitol8.901983Midt om nattenKim
LarsenEUMedley7.801983Pavarotti Gala ConcertLuciano PavarottiUKDECCA9.901991The dock of
the bayOtis ReddingUSAStax Records7.901968Picture bookSimply RedEUElektra7.201985RedThe
CommunardsUKLondon7.801987Unchain my heartJoe CockerUSAEMI8.201987 (I manually added the line breaks to prevent that showing as a single very long line!)
So you need to work out what is in the XML that you are interested in, and how you will "present" that in your resulting file - otherwise it's just a waste of your time and effort!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
MyPluginDllClass
{
MyClass _myClass {get; set} = new(this);
public MyPluginDllClass
{
}
public void Test1()
{
}
}
public MyClass
{
MyPluginDllClass _instanceMyPluginDllClass;
public MyClass(MyPluginDllClass param)
{
_instanceMyPluginDllClass = param;
}
public void Test2()
{
_instanceMyPluginDllClass.Test1();
}
}
Hello,
for being able to see all properties in the UI of MyPluginDllClass I would have to instantiate MyClass
as shown above in the properties area of MyPluginDllClass as follows:
MyClass _myClass {get; set} = new(this);
But this does not work, "this" seems to be unknown at this moment in time.
The goal is to use all member functions and properties of MyPluginDllClass within MyClass and to make all properties of MyClass visible in the property grid of the UI of the main application.
Any Ideas, hints what I am doing wrong or why this is the case.
I do not have access to the code of the main application.
MyPluginDllClass ist the interface to the main application.
|
|
|
|
|
You can't use new(this) in a property initializer as the initializer would require the whole object to be initialised by that point, and that cannot be the case because the constructor has not been called yet. And obviously, the constructor can't be called until all property and field initializers are complete!
So you would end up with a deadlock: the instance can't be created because to create the instance you need access to the fully constructed instance!
What you can do is move the initializer to the constructor:
public class Bar
{
public Bar(Foo foo) { }
}
public class Foo
{
public Bar bar {get; set;}
public Foo ()
{
bar = new(this);
}
}
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
While I appreciate all the syntactic shortcuts Anders and friends are giving us in the C# language, I feel it's getting a bit ridiculous. We're quickly approaching a point where the shortcuts are making the code unreadable and harder to understand for noobs in favor of making it faster to type.
I suspect the new shortcut is giving the OP trouble in understanding what's going on.
|
|
|
|
|
I agree - the same thing happened to C++ to the point where it's almost unreadable now.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
LOL. I think that's the primary reason why I don't do C++ anymore. That and someone has to pick up my code and maintain it after I'm gone and it's easier to find a C# dev (or Java dev we can convert) than it is to find a C++ dev for the team I'm on.
|
|
|
|
|
Is there a way to exclusively identify email-enabled public folders using Microsoft.Office.Interop.Outlook? EWS or PowerShell should not be used. The program should run on the workstation and utilize the installed Outlook.
Once I've found an email-enabled public folder, I should list the emails contained within it.
So far, I'm encountering difficulties. For instance, I have the following method:
static void ListPublicFolders(Outlook.Folder? folder, string indent)
{
if (folder != null)
{
foreach (object obj in folder.Folders)
{
if (obj is Outlook.Folder)
{
Outlook.Folder? subFolder = obj as Outlook.Folder;
if (subFolder != null && subFolder.DefaultItemType == Outlook.OlItemType.olMailItem)
{
Outlook.MAPIFolder? parentFolder = subFolder.Parent as Outlook.MAPIFolder;
string parentName = parentFolder != null ? parentFolder.Name : "Parent folder not found";
Console.WriteLine($"{indent}- {subFolder.Name}: {parentName}");
if (parentFolder != null)
{
Marshal.ReleaseComObject(parentFolder);
}
}
ListPublicFolders(subFolder, indent + " ");
if (subFolder != null)
{
Marshal.ReleaseComObject(subFolder);
}
}
}
}
}
The query
if (subFolder != null && subFolder.DefaultItemType == Outlook.OlItemType.olMailItem)
fails because subFolder.DefaultItemType returns the value Outlook.OlItemType.olPostItem, even though the public folder was set up as an email-enabled folder in Exchange.
Specifically, this is in Microsoft 365. In the Exchange admin center, when creating the folder, I explicitly checked the box for "Email-enabled." This action resulted in two additional options: "Delegation" and "Email properties." In "Email properties," I can specify an alias and a display name. By default, both fields are set to "Orders." Now, I expect the public folder to be email-enabled, with the email address orders@domain.tld.
I don't understand why Outlook is treating the folder incorrectly (I can only create posts and not send emails).
Perhaps someone can help me figure this out.
Thank you and best regards,
René
|
|
|
|
|
Hi All,
Stupid Question time. If a value is read to a textbox it is a String, to convert it to a value in the past I have done:
int Value = 0;
Value = Convert.ToInt16(textBox1.Text) I'm sure of it and then done maths and operations on the Value such as:
if(Value > 1000)
{
MessageBox.Show("Here!");
} But it won't run in VS2022 it returns a System.Format.Exception error. What have I done?
Glenn
modified 9-May-24 10:29am.
|
|
|
|
|
Don't use Convert as any error in the format of the input string will cause an exception.
Instead use int.Parse because you can check the result of the parse operation to see if you have entered a valid or invalid string.
From memory you should do the following
if (int.Parse(textBox1.Text, out int result))
{
}
else
{
}
|
|
|
|
|
You mean TryParse ; Parse returns the parsed value or throws an exception.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I checked that twice and still missed that (Doh)
|
|
|
|
|
What value is in the textbox? If it can't be parsed as a short using the current culture settings, then Convert.ToInt16 will throw an exception.
You should use TryParse[^] rather than the Convert method to avoid the "can't parse this" exception:
if (int.TryParse(textBox1.Text, out int value))
{
if (value > 1000)
{
MessageBox.Show("Here!");
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hmmm, I'm reading back a sensor, it's giving values all over the show!
private void button1_Click(object sender, EventArgs e)
{
if (int.TryParse(textBox1.Text, out int value))
{
if (value > 1000)
{
MessageBox.Show("Here!");
}
MessageBox.Show(value.ToString());
}
MessageBox.Show(value.ToString());
}
value is taken as 0, despite textBox1.Text having 1234.56...
|
|
|
|
|
glennPattonWork3 wrote: despite textBox1.Text having 1234.56... But 1234.56 is a Float/Double not an Int ; the two are quite different.
|
|
|
|
|
You are right, but even if make 1234 it still returns a 0...
|
|
|
|
|
Come on Glen, you need to show us the exact code, and the exact text that you are using.
|
|
|
|
|
Hi,
This is the code I have been trying to get all afternoon!
private void button1_Click(object sender, EventArgs e)
{
float ForceVal = 0;
<pre>
decimal.TryParse(weightTxt.Text, out decimal value);
if (value > 1000)
{
MessageBox.Show("Here!");
}
MessageBox.Show(value.ToString());
}
I think that should do it. I'm hacking together a test program that interfaces to a Phidget load cell in trying to measure a value that needs to converted to Newtons from Grams (I think!). I hate the fact I wasted so much time trying to get the value to be used out of a darned label. I haven't really done much Windows code (I'm an embedded guy really) since VS2008 was the new kid on the block and times like these it shows!
|
|
|
|
|
Did the TryParse call succeed, return true?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Sort of I had to:
decimal.TryParse(weightTxt.Text, out decimal value);
as I was messing with a floating point number, haven't coded in Windows for a while, man it shows!
|
|
|
|
|
Maybe try decimal.TryParse()? as the input is not an int.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Thanks, that appears to work!
|
|
|
|