|
I asked this question in another thread but no one answered it so I'm creating a new thread to see if someone can help. I need help with pass the value in the API to XElement in C#
Here the a piece of the API schema:
<xsd:element
name="EventStatus" type="xsd:string">
<xsd:annotation>
<xsd:documentation>Event Status</xsd:documentation>
</xsd:annotation>
</xsd:element>
Is this how that would be done?
new XElement("EventStatus", "Event Status"),
),
|
|
|
|
|
|
Bootzilla33 wrote: Is this how that would be done?
No.
From the fragment you have an 'element' which has an 'attribute' which has a 'name' of "name" (the two are not the same) and has a 'value' of "EventStatus".
Then you have another attribute also.
Then that element has other elements under it.
The documentation for XElement demonstrates some of this and more importantly demonstrates how you can print out the result so you can see it yourself.
XElement Class (System.Xml.Linq)[^]
You can look for other examples using XElement and XAttribute.
I want to emphasize that figuring out how to print these out is the most important thing you can learn. You need that even once you understand most of it.
|
|
|
|
|
Hello Community.
I have built an aplication wich interacts with a webserver as if it were a browsing user. It can log in, browse products and select a product to buy. Unfortunately when it comes to actually buying the product the server returns just the logintoken and the shopping cart is still empty.
I suppose the session cookie is not sent.
Is there any way I can debug the httpclienthandler? I am a beginner in Programming and I hoped there was a way similar to Chome debugger console.
best regards,
julian
|
|
|
|
|
Hi there,
I am experiencing issue with split method. When I try split something like this:
06302017;37;Denver; Other Non-Interest Expense from RI item 7.d. : Travel Expense=$88,799. Excess public-facing website address listings f rom
RC-M line 8.b.: sparkalpha.com, www.getsparkalpha.com, www.getsparkalpha.com, www.sparkalpha.com; 07282017;Other; RCM; M4b
The tab that occurs is not my doing but what actually happens with that mass of string and I believe it is causing my problem. It splits this in half into two different arrays. This is problematic because each item separated by a ';' corresponds to a field so it causes an out of bounds error. Any suggestions on how to get it to keep the 8 different strings within the split? Based on how my program is run, I cannot make a special case of this and expect it to work well.
By the way, I didn't write the line that I'm trying to split so I cannot fix it myself. I am grabbing a file from a website location and then splitting it so that the data can be used for a database.
Thank you for your help.
|
|
|
|
|
Split is unlikely to do what you want. It sounds like you are looking for a CSV reader.
Or, if you are using SQL Server, try the bcp utility.
|
|
|
|
|
I'm currently taking in the file through a Streamreader and then running a method which has the split method. It works for all other lines except that one. It's because of the tab that inconveniently gets thrown in. I'm trying to use C# right now. I will see if CSV reader is any better.
|
|
|
|
|
What are you trying to split it into? If you know what characters you want to split on then just add them to the splitter set.
|
|
|
|
|
I am trying to split it by ';'. This is a file that I'm reading in. 20170630;37;Denver;Other Non-Interest Expense from RI item 7.d. : Travel Expense=$88,799. Excess public-facing w ebsite address listings f rom
RC-M line 8.b.: sparkalpha.com, www.getsparkalpha.com, www.getsparkalpha.com, www.sparkcapitalone.com.;20170728;Explanations;Call;m1b
C# keeps bringing the RC-M down a line (tabs it which isn't in the file). It is screwing up my delimitation and causing an out of bounds error. This doesn't happen anywhere else in the file.
class Program
{
static void Main(string[] args)
{
string CurrentLine;
string FilePath = "C:\\12837.SDF.txt";
using (StreamReader sr = new StreamReader(FilePath))
{
while(!sr.EndOfStream)
{
CurrentLine = sr.ReadLine();
GetSplit(CurrentLine);
}
}
}
private static void GetSplit(string CurrentLine)
{
string[] array = CurrentLine.Split(';');
string first = array[0];
string second = array[1];
string third = array[2];
string four = array[3];
Console.WriteLine(first + " " + second + " " + third + " " + four);
}
}
}
This is the test code I'm running for reference and not the actual code I end up using but it still comes up no matter what. I tried to Replace it and I might just try to skip the line when it pops up. By the way, I have the comments in there because I'm trying to see how many indexes it actually brings in.
Here's what it looks like in console:
20170630 112837 Denver Other Non-Interest Expense from RI item 7.d. : Travel Expense=$88,799. Excess public-facing w ebsite address listings f rom
RC-M line 8.b.: sparkalpha.com, www.getsparkalpha.com, www.getsparkalpha.com, www.sparkcapitalone.com. 20170728 explanations RIE
Here's what the different line looks like:
20170630 112837 Denver Adjustment investment
|
|
|
|
|
WoodChuckChuckles wrote: C# keeps bringing the RC-M down a line I don't know what you mean by that, but I don't think C# will do anything with your data. When I run your code (using the text in your question) I get the following output:
1: 20170630
2: 37
3: Denver
4: Other Non-Interest Expense from RI item 7.d. : Travel Expense =$88,799.Excess public-facing website address listings fromRC-M line 8.b.: sparkalpha.com, www.getsparkalpha.com, www.getsparkalpha.com, www.sparkcapitalone.com.
5: 20170728
6: Explanations
7: Call
8: m1b
|
|
|
|
|
If there is some kind of invisible character you can always scrub your string first.
while(!sr.EndOfStream)
{
CurrentLine = sr.ReadLine();
GetSplit(CurrentLine.Replace("\t", "");
}
And you should always be careful accessing an array like, you might try defensive coding (try - catch) or iterating through the array using a foreach() that way you will not end up out of bounds. If you need certain indexed members of the array to print, wrap the code and catch out of bounds exceptions.
|
|
|
|
|
How do you scrub for an invisible char? I'm not experienced with that. I apologize.
|
|
|
|
|
It was a bit of hyperbole. Any time you are parsing string data you want to understand what possible user inputs could cause you problems and make sure your string is scrubbed before you split it. If there is a possibility of newline characters, carriage returns, or inconsistent tabs you want to address those. The simple solution is a string replace, there are more advanced strategies using regular expressions and such. But in favor of simplicity try something like this:
class Program
{
static void Main(string[] args)
{
string CurrentLine;
string FilePath = "C:\\12837.SDF.txt";
using (StreamReader sr = new StreamReader(FilePath))
{
while(!sr.EndOfStream)
{
string currentLine = sr.ReadLine();
GetSplit(currentLine.Replace("\t","").Replace("\r","").Replace("\n","");
}
}
}
private static void GetSplit(string CurrentLine)
{
string[] array = CurrentLine.Split(';');
string first = array[0];
string second = array[1];
string third = array[2];
string four = array[3];
Console.WriteLine(first + " " + second + " " + third + " " + four);
}
}
}
That is just a general idea on how to handle it. One thing you always want to be wary of is bad input, and the best way to deal with it is to aggressively control your strings by stripping out troublesome characters.
|
|
|
|
|
<stackpanel> <grid text="MyNotes" fontfamily="Arial" fontweight="SemiLight" fontsize="26"> <grid text="Sample application for Windows 8 course" fontfamily="Arial" fontweight="SemiLight" fontsize="18" textwrapping="Wrap">
<stackpanel> <textbox="mynotes" fontfamily="Arial" fontweight="SemiLight" fontsize="26"> <textbox="sample application="" for="" windows="" 8="" course"="" fontfamily="Arial" fontweight="SemiLight" fontsize="18" textwrapping="Wrap">
<stackpanel>
<stackpanel> <textblock text="MyNotes" fontfamily="Arial" fontweight="SemiLight" fontsize="26"> <textblock text="Sample application for Windows 8 course" fontfamily="Arial" fontweight="SemiLight" fontsize="18" textwrapping="Wrap">
modified 22-Sep-17 6:37am.
|
|
|
|
|
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.
Try it yourself, you may find it is not as difficult as you think!
If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
If you try them out, you'll find the answer. None of them.
This space for rent
|
|
|
|
|
var buffer = Windows.Security.Cryptography.CryptographicBuffer.ConvertStringToBinary( "The first sentence", Windows.Security.Cryptography.BinaryStringEncoding.Utf8); await Windows.Storage.FileIO.WriteBufferAsync(sampleFile, buffer);
var buffer = Windows.Security.Cryptography.CryptographicBuffer.ConvertStringToBinary( "The first sentence", Windows.Security.Cryptography.BinaryStringEncoding); await Windows.Storage.FileIO.WriteBufferAsync(sampleFile, buffer);
var buffer = Windows.Security.Cryptography.CryptographicBuffer.ConvertStringToBinary( "The first sentence", Windows.Security.Cryptography.BinaryStringEncoding.Utf8); await Windows.Storage.FileIO.ReadBufferAsync(sampleFile, buffer);
Var buffer = Windows.Security.Cryptography.CryptographicBuffer.ConvertStringToBinary( "The first sentence", Windows.Security.Cryptography.BinaryStringEncoding.Utf8); await Windows.Storage.FileIO(sampleFile);
|
|
|
|
|
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("example string"); I'd like to point out that a text-encoding is not the same as encryption. A text encoded in UTF8 is still a readable text.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Or ...
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(myString);
File.WriteAllBytes(path, bytes);
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Or ...
File.WriteAllText(sampleFile, stringContent, System.Text.Encoding.UTF8);
|
|
|
|
|
I was waiting for that. First class!
Regards,
Rob Philpott.
|
|
|
|
|
Thank you.
But why you have not posted it instead of waiting?
|
|
|
|
|
Ah well, the timing of my response is coincidental, I wasn't actually waiting until someone posted it, but just that that was the answer I was waiting for, being the best IMO. I read the thread about a minute after your answer.
Besides, I couldn't remember what the method was called and certainly couldn't be bothered to look it up. It's Friday and I'm just observing today, not participating.
Regards,
Rob Philpott.
|
|
|
|
|
Hi all,
In my first C# console application I want to avoid COM problems and therefore I'm registering the IOleMessageFilter to handle any threading errors.
Well this wouldn't be a question without the problem part so here we go:
public static void Register()
{
IOleMessageFilter newFilter = new MessageFilter();
IOleMessageFilter oldFilter = null;
int test = CoRegisterMessageFilter(newFilter, out oldFilter);
if (test != 0)
Debug.Fail("CoRegisterMessageFilter failed!");
else
_isRegistered = true;
}
This never goes past the Debug.Fail, but I can't understand why as the message is not very explanatory by itself:
---------------------------
Assertion Failed: Abort=Quit, Retry=Debug, Ignore=Continue
---------------------------
CoRegisterMessageFilter failed!
at SysConfig.MessageFilter.Register() in c:\...\MessageFilter.cs:line 42
at SysConfig.DTE.CreateDTE(Boolean ideVisible, Boolean suppressUI, Boolean userControl) in c:\...\DTE.cs:line 50
at SysConfig.Program.SysConfigM(Int32 i) in c:\...\Program.cs:line 163
at SysConfig.Program.SetNumberOfM() in c:\...\Program.cs:line 136
at SysConfig.Program.MenuHandler() in c:\...\Program.cs:line 48
at SysConfig.Program.Main(String[] args) in c:\...\Program.cs:line 26
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.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
---------------------------
What could happen?, searching about CoRegisterMessageFilter is very fun as it doesn't show a lot of information...
And... I've just copied the class from an example, and called Register()...
I'm trying to automate Visual Studio, in most cases the program flow works perfectly.
In some other cases it fails during calls to the Visual studio DTE...
In the manual from the manufacturer of the software I'm using they state that I should implement that COM Message Filter and they explain how to do it... Even they provide a sample that I've pasted directly into my code...
Anyone can see what's wrong given the message?
Should I add any dependency or similar thing into my project to get this working?
Being ultra-novice in C# I can't see where to search now...
And I truly would like not to add wait timers everywhere...
Thank you very much for your time and help.
|
|
|
|
|