Calendar notes in .xml

  • Thread starter Thread starter bojan.pikl
  • Start date Start date
B

bojan.pikl

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>
 
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
 
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
 
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
 
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
 
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
 
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).
 
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
 
Back
Top