|
<Root>
<Book>
<ID>A</ID>
<NAME>AA</NAME>
</Book>
<Book>
<ID>B</ID>
<NAME>BB</NAME>
</Book>
</Root>
I want to delete the parent node whose ID is "B" ... how can i do this ?? Thank you..
<Root>
<Book>
<ID>A</ID>
<NAME>AA</NAME>
</Book>
</Root>
|
|
|
|
|
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
xDoc.Load("<<give xml path here>>");
System.Xml.XmlElement xEle = (System.Xml.XmlElement)xDoc.DocumentElement.SelectSingleNode("//Book[ID = 'B']");
if (xEle != null)
{
xEle.ParentNode.RemoveChild(xEle);
}
I have not tested this should work
Check My other C# Posts
modified on Friday, May 9, 2008 10:24 AM
|
|
|
|
|
I am looking for a good method to keep my constants. Currently I have created a static class like this
public static class ConstantMessages
{
public static string ServerCommunicationFailure = "Message";
public static string AuthenticationFailure = "Message";
} I have many fields like this. Is this a good approach ? Or is there any alternative better approach for this ? I will be using the above class like the following
try
{
}
catch(Exception ex)
{
throw new CommunicationException(ConstantMessages.ServerCommunicationFailure);
}
|
|
|
|
|
I'd just use a settings class, that's what they are for. But, this does work just fine.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Christian Graus wrote: But, this does work just fine.
Thanks. Yes this works fine, but it tough to maintain. Each time a variable and it's value has to be added. Settings class looks good, I will give a try. There is going to be many settings which will be kept in this settings file, do you think it will slow down the accessing ?
|
|
|
|
|
Looking at this example it looks like there is a contradiction of ideas here. The class is called ConstantMessages, but the strings inside the class are being set up so that the application can modify them. If you want constant messages why not use:
public const string ServerCommunicationFailure = "Message";
This should give a performance benefit.
If I am wrong, someone please correct me so I can continue to learn.
Kalvin
|
|
|
|
|
Yes you are correct. I wrote that to explain the problem. This is not used exactly as it is.
|
|
|
|
|
However, the recomended way by MS is to use static readonly instead of const.
because of versioning issues.
if you use a const, every consumer that use your const will have the const value compiled down into the consumer code.
while if you access a readonly field, the consumer code will always get the values from the owner class.
in short, if you use consts and then ship a new version of the dll containing your consts, those changes will not affect already compiled code.
while if you use readonly fields, it will affect already compiled consumers..
|
|
|
|
|
Very valid points. Thanks for that Roger
|
|
|
|
|
What?! I've never seen that documented anywhere.
But I just tried it and it seems to be true.
That doesn't make sense.
Edit:
It's true of enum members as well.
I just looked through the specs and saw no mention of this functionality, but I see now that it does make (some) sense.
Among other things, const values are valid in case labels, and case labels must be unique. Changing a const value (or enum ) could make a previously-compiled switch invalid.
So, yeah, I think a best practice is; unless the value is an actual constant value like Pi, don't use public const , but use public readonly instead. Using private const , may not be as bad, but the next developer to come along may make your private s public . So use const sparingly.
Thanks for the tip.
modified on Monday, May 12, 2008 11:46 AM
|
|
|
|
|
Use an enum with Description attributes?
enum Messages
{
[Description("Message")]
ServerCommunicationFailure
,
[Description("Message")]
AuthenticationFailure
}
|
|
|
|
|
Thanks. I will take a look into that.
|
|
|
|
|
Hello all,
I have an array of a simple object type:
class EntryPoint
{
private widgets[];
public EntryPoint()
{
this.widgets = new widgets[1000]
...code here to populate 1000 widgets...
}
public Widgets[]
{
get { return this.widgets; }
}
}
class widgets
{
public string key;
public string value;
}
Now, elsewhere in the the application, I need to be able to extract values from the widgets[] from instances of EntryPoint . However I need to be able to do this using the string key, not the index number. So the question is how can I do this? A hacking but slow way is something like this:
public string GetWidgetValue(string key)
{
string returnValue = null;
foreach(widget w in this.widgets)
{
if (w.Key == key)
{
returnValue = w.value;
break;
}
}
return returnValue;
}
This would work, however as mentioned enumerating through the array for every value request could prove to be slow. So can anybody give me an idea of what to do?
A perfect solution would be to encapsulate the base array with something like a StringDictionary and therefore do something like this:
EntryPoint ep = new EntryPoint();
string value = ep.Widgets["widgetkey"];
So the question is, how to do this?
Obviously the sample above is a simplified version of the question, in reality I am working with an array that is generated by deserialising an XML file into a class generated by the XSD tool. This means that unless I ditch XSD for something else (any suggestions?) I am stuck with integer indexed arrays.
modified on Friday, May 9, 2008 7:23 AM
|
|
|
|
|
Quick ? What sort of subject line is that ?
The obvious solution is a Dictionary, that's what they are for.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Christian Graus wrote: Quick ? What sort of subject line is that ?
An extremely poor one, my error - we all make them from time to time
Christian Graus wrote: The obvious solution is a Dictionary, that's what they are for.
Yes, however as mentioned in the original post, I am working with classes generated by the XSD tool. As such I need underpin the stringdictionary with the simple arrays this tool creates and keep the array and the dictionary in synchronisation as code adds, removes, edits values. Unfortunately this is where I am not sure where to go or what to do.
|
|
|
|
|
WEll, you really cannot lookup by string unless you build a container that works that way. Why not just build a dictionary at the point of deserialising the list, then use that for lookups ?
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Well thats the idea, wrap the base array with a StringDictionary. However I need to keep the StringDictionary and base array in sync as code adds/edits/removes items regardless of whether this is done by string key on integer index.
So the question is how to do it?
|
|
|
|
|
I guess you need to write a class which contains an array and a dictionary, so that you only add/remove/access items via the interface of that class, which then ensures they are kept in sync
Or, you could use the Values collection of your dictionary as your plain vanilla list of items ?
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Christian Graus wrote: The obvious solution is a Dictionary
And they work brilliantly.
Excellence is doing ordinary things extraordinarily well.
|
|
|
|
|
Yes they do, however what I don't know how to do is encapsulate the XSD tool generate simple array with a StringDictionary while keeping everything in sync and not breaking the (de)serialiser, and therefore allow the following:
string val1 = widget[0]
string val2 = widget["mykey"]
Console.Writeline(val1 == val2)
Which would output true
|
|
|
|
|
If you're using XML, use an XmlDocument.
It sounds like your "XSD tool" is causing you more trouble than it's worth.
|
|
|
|
|
Hi
I have a ToolStripContainer where I only use the top and bottom. I set up serveral ToolStrips that I placed into the ToolStripContainer. The ToolStrips are small enough to fit all in one row (on a normal screen). Now, I like to show/hide these ToolStrips based on user action. And besides shwoing I also like to rearrange them. Now here is the problem. What ever I do they get mixed up and are not displayed at the Location I told them.
Basically I do this :
<br />
int x = 0;<br />
switch(_type) {<br />
case PropertiesItemMode.Text:<br />
toolStripText.Visible = true;<br />
toolStripMedia.Visible = false;<br />
toolStripMenu.Visible = false;<br />
toolStripPositionProperties.Visible = true;<br />
toolStripScaleProperties.Visible = true;<br />
<br />
x = 0;<br />
if (toolStripInfo.Visible) {<br />
x += toolStripInfo.Size.Width;<br />
}<br />
<br />
toolStripPositionProperties.Location = new Point(x, 0);<br />
x += toolStripPositionProperties.Size.Width;<br />
<br />
toolStripScaleProperties.Location = new Point(x, 0);<br />
x += toolStripScaleProperties.Size.Width;<br />
<br />
toolStripText.Location = new Point(x, 0);<br />
<br />
break;<br />
case PropertiesItemMode.Menu:<br />
toolStripText.Visible = false;<br />
toolStripMedia.Visible = false;<br />
toolStripMenu.Visible = true;<br />
toolStripPositionProperties.Visible = true;<br />
toolStripScaleProperties.Visible = false;<br />
<br />
x = 0;<br />
if (toolStripInfo.Visible) {<br />
x += toolStripInfo.Size.Width;<br />
}<br />
<br />
toolStripPositionProperties.Location = new Point(0, 0);<br />
x += toolStripPositionProperties.Size.Width;<br />
<br />
toolStripMenu.Location = new Point(150, 0);<br />
<br />
break;<br />
}<br />
<br />
Any idea what I am doing wrong here ?
Thanks
-Sebastian
|
|
|
|
|
Here's an example of what I'm doing.
public class MyObject1 {}
public class MyObject2 {}
public class MyData
{
public MyObject1 myObj1;
public MyObject2 myObj2;
public void SetData(MyObject1 ojb1, MyObject2 obj2)
{
myObj1 = obj1;
myObj2 = obj2;
}
}
public partial class MyForm
{
private MyObject1 myObj1;
private MyObject2 myObj2;
private void MyFunction()
{
MyData data = new MyData();
data.SetData(myObj1, myObj2);
}
}
I don't understand why this should generate an accessibility error during the compile.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
modified on Friday, May 9, 2008 7:26 AM
|
|
|
|
|
Well, this will not compile, as it happens, but once I fix the syntax errors, it builds just fine.
John Simmons / outlaw programmer wrote: public SetData(MyObject1 ojb1, MyObject2 obj2)
needs to be
public void SetData(MyObject1 obj1, MyObject2 obj2)
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Yeah, forgot the void, but it won't compile for me in my app (admittedly much larger and complex than the example I posted).
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|