|
Hello,
How can I obtain the value from a specific node in my XML file?
For example please consider the xml structure below. How would I for instance obtain "max" and "min" values for node
<xx atty="3" attz="0"> and in case it requires a different search method I would also like to know how to obtain the value at node
<vars var0="3" var1="0">0</vars>
?
Anxious to learn how to do this
Best Regards
Tom
="1.0"="utf-8"
<data device="xx" version="1">
<node1>
<xx atty="2" attz="3">
<max>0</max>
<min>0</min>
</xx>
<xx atty="3" attz="0">
<max>0</max>
<min>0</min>
</xx>
<xx atty="3" attz="1">
<max>0</max>
<min>0</min>
</xx>
</node1>
<node2>
<vars var0="0" var1="0">0</vars>
<vars var0="1" var1="0">0</vars>
<vars var0="2" var1="0">0</vars>
<vars var0="3" var1="0">0</vars>
<vars var0="4" var1="0">0</vars>
</node2>
</data>
|
|
|
|
|
LINQ is your friend when walking XML files.
There are a lot of examples using Linq queries over XML to get data out of the file. I'm too lazy right now to Google it or search here on CP so I'll leave that to you. But that should get you what you want.
|
|
|
|
|
How about this: //xx[@atty='3' and @attz='0']/*[name()='min' or name()='max']
|
|
|
|
|
Hey bimbambumbum,
that's my way of doing it, with this code you'll get the attributes to your specific node. Code is written for .Net Framework 3.5
Additionaly i posted the XML file so you can backtrace the work and derive your code from it.
All you need to have set up is
the xmlDoc and a xPathNavigator xmlNavi = xmlDoc.createNavi...
public DataTable LoadValueConfig(string ControlName, string ParentName, string FormName)
{
DataTable _dt = new DataTable();
_dt.Columns.Add("Name");
_dt.Columns.Add("Value");
_xmlNavi.MoveToRoot();
_xmlNavi.MoveTo(_xmlNavi.SelectSingleNode("WindowConfig"));
if (FormName != "") { _xmlNavi.MoveTo(_xmlNavi.SelectSingleNode(FormName)); }
_xmlNavi.MoveTo(_xmlNavi.SelectSingleNode(ParentName));
if (_xmlNavi.MoveTo(_xmlNavi.SelectSingleNode(ControlName)))
{
_xmlNavi.MoveTo(_xmlNavi.SelectSingleNode(ControlName));
_xmlNavi.MoveToFirstAttribute();
do
{
if (_xmlNavi.Value != "")
{
_dt.Rows.Add(_xmlNavi.Name, _xmlNavi.Value);
}
} while(_xmlNavi.MoveToNextAttribute());
}
return _dt;
}
XML File:
<WindowConfig>
<kopiefrmHauptfenster Position="460:21" Size="878:1170" WindowState="Normal">
<checkBoxItem1 chBoxCheck="True" />
<bsAdminTest Active="False">
<switchButton1 switch="False" />
<richTextBoxEx1 Text="" />
</bsAdminTest>
<Project Active="True">
<navigationPane1 CheckedButton="Projekte" Expanded="True" />
<expandablePanel1 Expanded="True" />
</Project>
</kopiefrmHauptfenster>
</WindowConfig>
Edit: added the comments for more Explanation
Ist probably not the best and most comfortable solution but int works for me, optimizations appreciated
if(this.signature != "")
{
MessageBox.Show("This is my signature: " + Environment.NewLine + signature);
}
else
{
MessageBox.Show("404-Signature not found");
}
modified 5-Dec-13 10:13am.
|
|
|
|
|
Thanks gentlemen..this looks like a great starting point! Appreciate it
|
|
|
|
|
Is It Possible that we store a field of a table from a database and store it in static variable..
Plzzzz Help Me out this..
further I want to use that variable in if-else condition,
for example:
if (datafield(Gender)="male")
open maleForm
else
Open FemaleForm..
Tarique Moin Shaikh
|
|
|
|
|
The simple answer is yes you can. Static does not mean you cannot change the value of the variable.
Your idea of using the variable in the if statement is correct.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
But Sir I failed to do it.
My Detailed Problem is:
I have two forms (Male and Female).
and I have a data base in which there is a field(Gender)
i want to extract the value of the gender field i-e male or female in a variable TEMP
is this possible that I use TEMP in if-else condition As Below??
if(TEMP=="Male")
{
Messagebox.show("Hello Mister");
}
else
{
Messagebox.show("Hello Miss");
}
If Possible then plz give me the exhact code..
|
|
|
|
|
Sorry I get paid to write code, the only thing I can suggest is you get a book and work through the examples or find some tutorials. You do not have enough knowledge to understand the answers you are getting.
When searching for tutorials you should specify your database and what platform you are intending to use Winforms/ASP/WPF etc.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
As Mycroft has said, yes you can.
However, you probably don't want to, unless you have only one person or gender in your database.
If you make it static, you only get one per application, which makes it impossible to expand things later to support motte than one record at a time. Normally, this kind of information would be stored as part of a class instance identifying a person, rather than as a static variable.
Under normal circumstances, it is evidence of poor design to have to use static variables for something like this, and will generally cause you significant problems later.
|
|
|
|
|
Sorry Mr.OriginalGriff but I dont understand.
Can U plzz elaborate it??
With Practical or coding??
|
|
|
|
|
Not really, because the problem seems to be a bit more fundamental than you think - I'm not at all sure you have understood the basics of OOPs design (and without that, nothing I write is going to be understandable)
Plus, from the tiny amount of code you provide, I just don't have enough context to write anythign that woudl be useful to you.
How about you go back a couple of stages and explain what you are trying to do? Not just the brief bit you have described in your question, but an overview of the whole project and how you are planning on implementing it? (not too much detail, a good paragraph should do)
|
|
|
|
|
As others have already pointed out, that is not the way to do it. You need to process this decision for each database record so it would be completely wrong to store that value in a variable, static or otherwise. The code you have shown above is correct, where datafield(Gender) is a field in the current database record.
Veni, vidi, abiit domum
|
|
|
|
|
Yes, you can do it. Static keyword means, you can not change value but refer it.
|
|
|
|
|
Hello-
I'm fairly new to C# (classic ASP guy), and I'm having a heckuva time with this. I am hoping someone can point out my flawed logic and/or syntax.
I first need to ascertain if the current article group is one of several that need to be treated differently. If it is, it defines the var "strbody" using a complete value as retrieved from the database. If it is not one of those special article groups, then I have to do some string manipulation to format the retrieved value before defining and displaying it.
My string manipulation code is flawless, but neither of the strbody vars I define in my if/then/else block is recognized when I call it below the code block???
@{
string group = Model.ArticleGroupName;
if (group.Contains("Spacial Orientation")||group.Contains("Topography")||group.Contains("Osteology")||group.Contains("Angiology")||group.Contains("Neurology")||group.Contains("Myology")||group.Contains("Radiology")||group.Contains("Misc. Drawings")||group.Contains("Clinical Testing"))
{
var strbody = item.ShortBody;
}
else
{
string s = item.ShortBody;
string sLess = s.Remove(0, 12);
int index = sLess.IndexOf("Summary");
var strbody = (sLess.Substring(index + 8));
}
}
@strbody
That code results in the following error:
\Plugins\FoxNetSoft.Articles\Views\ArticleRead\List.cshtml(76): error CS0103: The name 'strbody' does not exist in the current context
I am new to this, so please don't hesitate to chastise me for doing dumb stuff...I need to learn!
Thanks,
Steve
|
|
|
|
|
Declare strbody outside of the if/else block, then assign it inside the block. As your code stands, the scope of the variable belongs inside the relevant if/else part. This is just standard behaviour for C#; it's the reason you have to define it twice in the if/else block right now - they are actually different variables. The fact that you can use the same name here is irrelevant.
|
|
|
|
|
Thanks for the response!
I previously tried declaring the strbody before the if statement as you suggested, but when I did, I got this error:
A local variable named 'strbody' cannot be declared in this scope because it would give a different meaning to 'strbody', which is already used in a 'parent or current' scope to denote something else
Learning C# is really frustrating!Confused | I could have done this exact thing with VBScript in about 2 minutes..and 8 less lines of code...
|
|
|
|
|
Steve Embry wrote: A local variable named 'strbody' cannot be declared in this scope because it would give a different meaning to 'strbody', which is already used in a 'parent or current' scope to denote something else
Looks like that variable is being defined somewhere else in the code; in other words you defined a variable with that exact same spelling and then tried to define it again somewhere else in the code. Try the previous suggestion of defining "strbody" before the statement and use the Find function (if you're using VS, navigate to Edit > Find and Replace > Quick Find) to try to locate all instances of that variable name, and see if maybe you used it for a different variable. Then just change the name (but, if you do, remember to update all references to the changed variable).
=======================
Every experience in life is a lesson to be learned
A. Stevens
B.S., Computer Science
|
|
|
|
|
While you have the answer to your question there is other points that I feel needs making.
I shudder with horror whenever I see a test against string content, I automatically assume the string can be edited by a user and you are screwed.
Next is the question of expanding criteria - what are you going to do when they add another term? And you know they will.
Built a construct such as a key word table when flags to define the function where it applies. The you can build a function that performs the test and return a boolean. This can be used in multiple places if required.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hello-
I'm fairly new to C# (classic ASP guy), and I'm having a heckuva time with this. I am hoping someone can point out my flawed logic.
I first need to ascertain if the current article group is one of several that need to be treated differently. If it is, it defines the var "strbody" using a complete value as retrieved from the database. If it is not one of those special article groups, then I have to do some string manipulation to format the retrieved value before defining and displaying it.
My string manipulation code is flawless, but neither of the strbody vars I define in my if/then/else block is recognized when I call it below the code block???
@{
string group = Model.ArticleGroupName;
if (group.Contains("Spacial Orientation")||group.Contains("Topography")||group.Contains("Osteology")||group.Contains("Angiology")||group.Contains("Neurology")||group.Contains("Myology")||group.Contains("Radiology")||group.Contains("Misc. Drawings")||group.Contains("Clinical Testing"))
{
var strbody = item.ShortBody;
}
else
{
string s = item.ShortBody;
string sLess = s.Remove(0, 12);
int index = sLess.IndexOf("Summary");
var strbody = (sLess.Substring(index + 8));
}
}
@strbody
That code results in the following error:
\Plugins\FoxNetSoft.Articles\Views\ArticleRead\List.cshtml(76): error CS0103: The name 'strbody' does not exist in the current context
I am new to this, so please don't hesitate to chastise me for doing dumb stuff...I need to learn!
Thanks,
Steve
|
|
|
|
|
"strbody" needs to be declared outside of the if statement, so that you can access it outside the scope of the if statement.
@{
string group = Model.ArticleGroupName;
string strbody = string.Empty;
if (group.Contains("Spacial Orientation") || group.Contains("Topography") || group.Contains("Osteology") || group.Contains("Angiology") || group.Contains("Neurology") || group.Contains("Myology") || group.Contains("Radiology") || group.Contains("Misc. Drawings") || group.Contains("Clinical Testing"))
{
strbody = item.ShortBody;
}
else
{
string s = item.ShortBody;
string sLess = s.Remove(0, 12);
int index = sLess.IndexOf("Summary");
strbody = (sLess.Substring(index + 8));
}
}
@strbody
|
|
|
|
|
Thanks for the response!
I tried declaring the strbody as you suggested previously, but when I did, I got this error:
A local variable named 'strbody' cannot be declared in this scope because it would give a different meaning to 'strbody', which is already used in a 'parent or current' scope to denote something else
Learning C# is really frustrating! I could have done this exact thing with VBScript in about 2 minutes..and 8 less lines of code...
|
|
|
|
|
Inside your if statement where it says "var strbody = ..." should now only be "strbody = ..." since you already declared the variable outside of the if statement. If you look at the modified code I provided in my previous post you will see that I am just assigning a value to the strbody variable inside of the if/else statement.
|
|
|
|
|
I spotted that moments after submitting my reply...
It's working perfectly now...thanks man!
|
|
|
|
|
Is there any benefit to adding BEGIN TRANSACTION; and COMMIT; to the beginning and end of a parameterized query in C#. Also, is there any real advantage to stored procedures vs parameterized queries?
For Example:
using (SqlConnection dbConnection = new SqlConnection(GSettings.SQLConnectionString))
{
dbConnection.Open();
for (int i = 0; i < DBUpdateList.Count; i++)
{
string selectSQL = "BEGIN TRANSACTION; UPDATE TABLE SET Dog = <a href="/Members/dog">@Dog</a>; COMMIT;";
using (SqlCommand dbCommand = new SqlCommand(selectSQL, dbConnection))
{
dbCommand.Parameters.AddWithValue("<a href="/Members/dog">@Dog</a>", "Poodle");
dbCommand.ExecuteNonQuery();
}
}
}
|
|
|
|