By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,916 Members | 1,286 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,916 IT Pros & Developers. It's quick & easy.

Calendar notes in .xml

P: n/a
Hi, I am making a simple calendar. I would like to have the ability of
notes. I was thinking of using this kind of notes.xml file. I am not
sure how to operate with nodes. I just need to access something like
SelectNode("/NOTES/y"+Year+"/m"+Month+"/d"+Day); (I'd get
/NOTES/y2006/m5/d18) and than I'd read what is in there. Can anyone
help please?
<NOTES>
<y2006>
<m4>
<DAYS>1,2,13</DAYS>
<DESCRIPTION>
<d1>New Year</d1>
<d2>Day after New Year</d2>
<d13>Special day :)</d13>
</DESCRIPTION>
</m4>
<m5>
<DAYS>1,30</DAYS>
<DESCRIPTION>
<d1>New Year</d1>
<d30>Special day :)</d30>
</DESCRIPTION>
</m5>
</y2006>
<y2007>
<m2>
<DAYS>5,28</DAYS>
<DESCRIPTION>
<d5>Service</d5>
<d28>Change tyers.</d28>
</DESCRIPTION>
</m2>
</y2007>
</NOTES>

Jul 29 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
bo********@gmail.com wrote:
Hi, I am making a simple calendar. I would like to have the ability of
notes. I was thinking of using this kind of notes.xml file. I am not
sure how to operate with nodes. I just need to access something like
SelectNode("/NOTES/y"+Year+"/m"+Month+"/d"+Day); (I'd get
/NOTES/y2006/m5/d18) and than I'd read what is in there. Can anyone
help please?
Have a look at the XmlDocument class. It provides the two methods called
SelectSingleNode and SelectNodes that you can pass your XPath expression
to. Does that help?
<NOTES>
<y2006>
<m4>
Instead of naming your elements like that, maybe it would be nicer to
use attributes? Like:

<Notes>
<Note Year="2006" Month="4" Day="1">New Year</Note>
<Note Year="2006" Month="4" Day="2">Day after New Year</Note>
<Note Year="2006" Month="4" Day="13">Special day :)</Note>
</Notes>

... you can query that with XPath too:
/Notes/Note[Year="2006" and Month="4" and Day="13"]

hth,
Max
Jul 29 '06 #2

P: n/a
Markus Stoeger wrote:
/Notes/Note[Year="2006" and Month="4" and Day="13"]
that should have been:
/Notes/Note[@Year="2006" and @Month="4" and @Day="13"]

Max
Jul 29 '06 #3

P: n/a
Hi could someone provide more information and perhaps an example.
Thank you guys

Jul 30 '06 #4

P: n/a
bo********@gmail.com wrote:
Hi could someone provide more information and perhaps an example.
Thank you guys
Have you read the MSDN docs for the methods I mentioned? They provide
lots of information and examples.

Max
Jul 30 '06 #5

P: n/a
Hi, yes I have but I am not sure.

My code looks like:
//Start code
XmlDocument doc = new XmlDocument();
doc.Load("notes.xml");

XmlNodeList nodeList;
XmlNode root = doc.DocumentElement;

nodeList=root.SelectNodes("/Notes/note[@Year="+this.Year.ToString()+"]");

//Save notes to a. A will be displayed later.
foreach (XmlNode note in nodeList)
{
a=note.InnerText.ToString();
}
//End code

And it does not works. The XML looks like you suggested. I am not sure
about XPath. I think it is not correct. Could you tell me some more
info about this?

Thank you,
Bojan

Aug 1 '06 #6

P: n/a
bo********@gmail.com wrote:
Hi, yes I have but I am not sure.

My code looks like:
nodeList=root.SelectNodes("/Notes/note[@Year="+this.Year.ToString()+"]");
XPath is case sensitive. Are you sure your Xml element is called "note"
and not "Note"? Makes a difference! Otherwise it looks good.
//Save notes to a. A will be displayed later.
foreach (XmlNode note in nodeList)
{
a=note.InnerText.ToString();
What is 'a'? Are you sure you don't need '+=' instead of '='?
And it does not works. The XML looks like you suggested. I am not sure
about XPath. I think it is not correct. Could you tell me some more
info about this?
Here is a short introduction to xpath that explains everything one has
to know about about it: http://www.w3schools.com/xpath/xpath_intro.asp

hth,
Max
Aug 1 '06 #7

P: n/a
Hi Markus,

it now works. Thank you for helping me.

Now I have to make a method that will insetred new notes into XML
database.
Could you tell me some methods for this?

Thanks again,
Bojan

Aug 4 '06 #8

P: n/a
I found an example:

XmlNode root = doc.DocumentElement;
XmlElement elem = doc.CreateElement("note");
elem.InnerText="Visit doctor at 5 PM";
root.InsertAfter(elem, root.FirstChild);

But I dont know how to setup a parametrs (Year, Month, Day).

Aug 4 '06 #9

P: n/a
bo********@gmail.com wrote:
I found an example:

XmlNode root = doc.DocumentElement;
XmlElement elem = doc.CreateElement("note");
elem.InnerText="Visit doctor at 5 PM";
root.InsertAfter(elem, root.FirstChild);

But I dont know how to setup a parametrs (Year, Month, Day).
These values are called xml attributes. Have a look at the documentation
of the method XmlElement.SetAttribute(string name, string value):

elem.SetAttribute("Year", "2006");

There's also GetAttribute to read the value back.

hth,
Max
Aug 4 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.