|
Make 'num an optional parameter that provides a default value which the caller can override:
public static int defaultNumValue = 10;
public class Repo
{
public List<string> GetData(int num = defaultNumValue)
{
var results = new List<string>();
for (int x = 0; x < num; x++)
{
results.Add($"Result{x}");
}
return results;
}
}
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
Greetings,
Do you know of any C#/NET libraries to validate an XML file with Schematron.
I searched but couldn't find anything.
Thanks for your help.
Anthony
|
|
|
|
|
|
I have a project that will end up language dependent. I am also fairly new to XML in terms of reading it from files. I need to use XML for information used by the project and that information will be available in several languages. So the basic structure of the XML is this:
<InfoNode>
<Language ID="enUS">
<DataClassNode>
<Name>SomeName</Name>
<Abbrev>SN</Abbrev>
<Description>Some Description of what this represents</Description>
</DataClassNode>
</Language>
</InfoNode>
The code that does this and then the code that deserializes the information is this:
public static DataClassNode Deserialize(XmlReader reader, CultureInfo culturalLanguage)
{
XmlReader found = null;
XmlReader _default = null;
while (found == null)
{
if (reader.ReadToFollowing("Language"))
{
if (reader.MoveToAttribute("ID"))
{
string id = reader.ReadContentAsString();
reader.MoveToElement();
if (id.Equals(culturalLanguage.Name))
{
found = XmlReader.Create(reader.ReadElementContentAsString());
}
else if (id.Equals(DefaultCulture.Name))
{
_default = XmlReader.Create(reader.ReadInnerXml().ToStream());
}
}
}
else
break;
}
if (found == null)
{
if (_default == null)
throw new Exception("Default Language Not Found!");
else
found = _default;
}
DataContractSerializer serializer = new DataContractSerializer(typeof(DataClassNode));
DataClassNode flagtag = (DataClassNode)serializer.ReadObject(found);
if (flagtag == null)
throw new Exception("Cannot deserialize DataClassNode Object!");
return flagtag;
}
I have been able to navigate to the language and make sure the one needed is found. Now I need to populate a class instance with the data inside the language node. I am using DataContractSerializer so I want a new XMLreader instance that contains the content of the language node ONLY. Not sure how to do this though. I know how to read strings etc... just not sure how to read the contents of the language node. I have tried what is above. I have tried InnerXML and OuterXML. Both of these return "None" as my XML. My comments point to where my knowledge falls short.
modified 20-Apr-23 10:00am.
|
|
|
|
|
I think you should be using "XPath navigation".
Select Nodes Using XPath Navigation | Microsoft Learn
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
The XmlReader is pretty low level, and I would not recommend you use it.
Better options:
- Use XDocument[^]. The examples on the page are focusing on creating the document, but use the static Load or Create methods when you already have the file.
- Make a full C# class representing your XML and use the DataContract serializer - then get your data from there using standard C# code.
XmlReader is very good at dealing with XML where you do not want to keep everything in memory - if that is a requirement, then you might have made a mistake somewhere earlier in the project.
That said, the XmlNodeReader[^] is doing what you are asking for.
|
|
|
|
|
Thanks,
I used a mix of XmlReader and XmlDocument to resolve it so it is similar to your suggestion.
|
|
|
|
|
Banging my head on a likely simple problem...
I'm getting a 405 on a WebSocket connection with WSS but not WS.
Vanilla project in VS with this code. Starting with HTTP works. Starting under HTTPS faults 405.
public static class Program {
public static void Main (string [] args) {
var builder = WebApplication.CreateBuilder ( args );
var app = builder.Build ();
app.UseWebSockets ();
app.MapGet ( "/" , (HttpContext http)
=> http.Response.WriteAsync ($$"""
<script>
self.Socket = new WebSocket(`${location.origin.replace("http", "ws")}/socket`);
</script>
""" ) );
app.MapGet ( "/socket" , Connect);
app.Run ();
}
static void Connect (HttpContext HttpContext) {
_ = HttpContext.WebSockets.AcceptWebSocketAsync ().Result;
}
}
modified 14-Apr-23 11:30am.
|
|
|
|
|
If you google "405 error", you get a description of why it occurs:
How to Fix HTTP Error 405 Method Not Allowed: 11 Methods[^]
But the most likely reason is simple: the remote site does not support HTTPS - so HTTP requests succeed, but secure ones fail. The only cure for that would be to contact the remote admins, and ask them to support HTTPS (which will require a valid certificate). Investigating that is where I'd start.
"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!
|
|
|
|
|
This is all in my own environment and https works on the site/server but not with sockets (wss).
This is (basically) code that I had running fine on .Net6, so I'm thinking there could be a config setting. I discovered it when I started upgrading (to WebApplication ) some old (working) apps.
It also works (WSS) when I'm running Fidder. If I close Fiddler, it quits working.
|
|
|
|
|
OriginalGriff wrote: does not support HTTPS - so HTTP requests succeed, but secure ones fail.
I don't think so.
The 405 is a HTTP error. So the HTTP server is responding to the request with that error.
The 'S' means that the protocol is actually HTTP (and thus TCP) with SSL (or perhaps TLS) in place.
The SSL protocol runs on the TCP layer and not the HTTP layer. If SSL is not in place (TCP) then for client server two things can happen
1. The client expects SSL but server is not doing it: Then it results in a timeout in the connection for the client because the client is expecting a response (the SSL protocol) which never happens.
2. The client is not using SSL but the server is: In this case the server ends up closing the socket because the the client never sent the expected SSL protocol messages. Client sees error message along the lines of the 'remote end' closed the socket. But it will not show up until the client attempts to use the socket (so connection worked but then later usage fails.)
|
|
|
|
|
I think I'm getting closer. I found a change in Edge that says it no longer uses the Windows certs - but manages its own. It doesn't seem to pick up my/VS self-gen'ed dev cert. Going to try to figure out how to do a manual import and see if that helps.
|
|
|
|
|
How to get XSI:type in inner class while converting to XML
|
|
|
|
|
You already posted this question in Q&A. There's no need to post it again.
What you do need to do is provide more information about what it is that you're actually trying to do here. That question could mean many things and the solution will depend on what it is that you're trying to do here.
|
|
|
|
|
I want to run a these commands in parallel and get the results as each finishes.
private void Load()
{
_apiProxy.GetNavigationItems(NavigationItemType.Company);
_apiProxy.GetNavigationItems(NavigationItemType.Employee);
_apiProxy.GetNavigationItems(NavigationItemType.Project);
}
What's the right way to do this?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
modified 7-Apr-23 1:17am.
|
|
|
|
|
Why not use three separate await Task.Run commands in sequence?
"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!
|
|
|
|
|
I always thought await returns back to the caller, so at the first await, the rest are not fired until the previous one completed
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Sorry - I didn't see the "in parallel" bit in your original post.
"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!
|
|
|
|
|
NP, so what's the best way to run them in parallel and wait for results from each one separatly?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
|
As in:
private void Bar()
{
Task.Run(() => { Thread.Sleep(2000); Console.WriteLine("1 done"); Thread.Sleep(2000); }).ContinueWith((x) => { Console.WriteLine("1 complete"); });
Console.WriteLine("one done");
Task.Run(() => { Thread.Sleep(2000); Console.WriteLine("2 done"); Thread.Sleep(2000); }).ContinueWith((x) => { Console.WriteLine("2 complete"); }); ;
Console.WriteLine("two done");
Task.Run(() => { Thread.Sleep(2000); Console.WriteLine("3 done"); Thread.Sleep(2000); }).ContinueWith((x) => { Console.WriteLine("3 complete"); }); ;
Console.WriteLine("all done");
}
"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!
|
|
|
|
|
In this case you can just do:
var companyTask = _apiProxy.GetNavigationItems(NavigationItemType.Company);
var employeeTask = _apiProxy.GetNavigationItems(NavigationItemType.Employee);
var projectTask = _apiProxy.GetNavigationItems(NavigationItemType.Project);
var companies = await companyTask;
var employees = await employeeTask;
var projects = await projectTask;
Of course it is up to the apiProxy (or any method it calls) if it will actually run concurrently.
modified 7-Apr-23 8:29am.
|
|
|
|
|
Kevin Marois wrote: I want to run a these commands in parallel and get the results as each finishes.
First of course you need to figure out how exactly you are going to collect the results. Additional to that you might also consider what you are going to do with the results.
Collecting the results itself has nothing to do with running them in parallel but it does impact how you encapsulate what it is exactly that you want each 'task' to do.
For example if each task just returned a boolean and you wanted to return that to the caller then you need a structure that holds a boolean and something that tells you which task returned which boolean. It can get way more complicated than that. For example if each one returns data in a different format.
Probably a really good idea also to consider what happens if there is an error. Could be an expected error or an unexpected error.
|
|
|
|
|
Assuming you have a list of tasks which return the same type, and you want to process the task results in the order in which they complete, then Stephen Toub has you covered:
Processing tasks as they complete - .NET Parallel Programming[^]
For a small number of tasks, using Task.WhenAny is probably good enough:
var tasks = new List<Task<T>>
{
_apiProxy.GetNavigationItems(NavigationItemType.Company),
_apiProxy.GetNavigationItems(NavigationItemType.Employee),
_apiProxy.GetNavigationItems(NavigationItemType.Project)
};
while (tasks.Count != 0)
{
var t = await Task.WhenAny(tasks);
tasks.Remove(t);
T result = await t;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi,
How can I have a regex for my .NET app's user password with the following:
Password MUST be a minimum of 10 AND maximum of 50
It should NOT contain space.
It should be MIXED alphabet (upper + lower), numeric and following special characters !@#$%^&*-_+=~
Thanks,
Jassim
www.softnames.com
|
|
|
|