EDN Admin
Well-known member
Hello together,
Ive tried the following code with C# and just have one question left.
Why do I need the following code?
<pre class="prettyprint" style=" static void Main(string[] args)[/code]
I know that this is for a parameter, type string (array) and args is for the values, but I still dont get the purpose of this the part between the brackets, especially because it runs also with main(). As far as I know from previous programming languages,
with args you expect values, which leads me to the question, from what? The xml file is called inside the main() method.<br/>
Can you may enlighten me?
Thanks in advance & regards
King Julien
<pre class="prettyprint" style=" using System;
using System.Xml;
using System.Xml.XPath;
using System.Threading;
namespace UseXPathNavigator
{
class BooksXmlNavigator
{
[STAThread]
static void Main(string[] args)
{
try
{
//this is a string representing where the xml file is located
string xmlFileName = "C:\Users\test\Desktop\books.xml";
// create an XPathDocument object
XPathDocument xmlPathDoc = new XPathDocument(xmlFileName);
// create a navigator for the xpath doc
XPathNavigator xNav = xmlPathDoc.CreateNavigator();
//navigate and print the document
NavigateBooksXml(xNav);
//navigate and print all the titles
FindAllTitles(xNav);
//navigate the xml doc and search for books that are in the category It
FindBooksByCategory(xNav, "It");
//To keep the screen up and waiting for the user to hit return
Console.Read();
}
catch (XmlException e)
{
Console.WriteLine("Exception: " + e.ToString());
Console.Read();
}
}
public static void NavigateBooksXml(XPathNavigator p_xPathNav)
{
// move to the root and the first element - <books>
p_xPathNav.MoveToRoot();
p_xPathNav.MoveToFirstChild();
// move to first <book> element
p_xPathNav.MoveToFirstChild();
Console.WriteLine("Printing contents of books.xml:");
//begin looping through the nodes
do
{
// list attribute;
if (p_xPathNav.MoveToFirstAttribute())
{
Console.WriteLine(p_xPathNav.Name + "=" + p_xPathNav.Value);
// go back from the attributes to the parent element
p_xPathNav.MoveToParent();
}
//display the child nodes
if (p_xPathNav.MoveToFirstChild())
{
Console.WriteLine(p_xPathNav.Name + "=" + p_xPathNav.Value);
while (p_xPathNav.MoveToNext())
{
Console.WriteLine(p_xPathNav.Name + "=" + p_xPathNav.Value);
}
p_xPathNav.MoveToParent();
}
} while (p_xPathNav.MoveToNext());
}
public static void FindAllTitles(XPathNavigator p_xPathNav)
{
//run the XPath query
XPathNodeIterator xPathIt = p_xPathNav.Select("//book/title");
//use the XPathNodeIterator to display the results
if (xPathIt.Count > 0)
{
Console.WriteLine("");
Console.WriteLine("The catalog contains the following titles:");
//begin to loop through the titles and begin to display them
while (xPathIt.MoveNext())
{
Console.WriteLine(xPathIt.Current.Value);
}
}
else
{
Console.WriteLine("No titles found in catalog.");
}
}
public static void FindBooksByCategory(XPathNavigator p_xPathNav,string p_Category)
{
string query = "//book[@category=" + p_Category + "]";
XPathNodeIterator xPathIt = p_xPathNav.Select(query);
//use the XPathNodeIterator to display the results
if (xPathIt.Count > 0)
{
Console.WriteLine("");
Console.WriteLine("The following books are " +
"in the {0} category:", p_Category);
while (xPathIt.MoveNext())
{
Console.WriteLine(xPathIt.Current.Value);
}
}
else
{
Console.WriteLine("No books found in the {0} category", p_Category);
}
}
}
}[/code]
<br/>
<
Regards / Freundliche Grüsse King Julien
<br/>
View the full article
Ive tried the following code with C# and just have one question left.
Why do I need the following code?
<pre class="prettyprint" style=" static void Main(string[] args)[/code]
I know that this is for a parameter, type string (array) and args is for the values, but I still dont get the purpose of this the part between the brackets, especially because it runs also with main(). As far as I know from previous programming languages,
with args you expect values, which leads me to the question, from what? The xml file is called inside the main() method.<br/>
Can you may enlighten me?
Thanks in advance & regards
King Julien
<pre class="prettyprint" style=" using System;
using System.Xml;
using System.Xml.XPath;
using System.Threading;
namespace UseXPathNavigator
{
class BooksXmlNavigator
{
[STAThread]
static void Main(string[] args)
{
try
{
//this is a string representing where the xml file is located
string xmlFileName = "C:\Users\test\Desktop\books.xml";
// create an XPathDocument object
XPathDocument xmlPathDoc = new XPathDocument(xmlFileName);
// create a navigator for the xpath doc
XPathNavigator xNav = xmlPathDoc.CreateNavigator();
//navigate and print the document
NavigateBooksXml(xNav);
//navigate and print all the titles
FindAllTitles(xNav);
//navigate the xml doc and search for books that are in the category It
FindBooksByCategory(xNav, "It");
//To keep the screen up and waiting for the user to hit return
Console.Read();
}
catch (XmlException e)
{
Console.WriteLine("Exception: " + e.ToString());
Console.Read();
}
}
public static void NavigateBooksXml(XPathNavigator p_xPathNav)
{
// move to the root and the first element - <books>
p_xPathNav.MoveToRoot();
p_xPathNav.MoveToFirstChild();
// move to first <book> element
p_xPathNav.MoveToFirstChild();
Console.WriteLine("Printing contents of books.xml:");
//begin looping through the nodes
do
{
// list attribute;
if (p_xPathNav.MoveToFirstAttribute())
{
Console.WriteLine(p_xPathNav.Name + "=" + p_xPathNav.Value);
// go back from the attributes to the parent element
p_xPathNav.MoveToParent();
}
//display the child nodes
if (p_xPathNav.MoveToFirstChild())
{
Console.WriteLine(p_xPathNav.Name + "=" + p_xPathNav.Value);
while (p_xPathNav.MoveToNext())
{
Console.WriteLine(p_xPathNav.Name + "=" + p_xPathNav.Value);
}
p_xPathNav.MoveToParent();
}
} while (p_xPathNav.MoveToNext());
}
public static void FindAllTitles(XPathNavigator p_xPathNav)
{
//run the XPath query
XPathNodeIterator xPathIt = p_xPathNav.Select("//book/title");
//use the XPathNodeIterator to display the results
if (xPathIt.Count > 0)
{
Console.WriteLine("");
Console.WriteLine("The catalog contains the following titles:");
//begin to loop through the titles and begin to display them
while (xPathIt.MoveNext())
{
Console.WriteLine(xPathIt.Current.Value);
}
}
else
{
Console.WriteLine("No titles found in catalog.");
}
}
public static void FindBooksByCategory(XPathNavigator p_xPathNav,string p_Category)
{
string query = "//book[@category=" + p_Category + "]";
XPathNodeIterator xPathIt = p_xPathNav.Select(query);
//use the XPathNodeIterator to display the results
if (xPathIt.Count > 0)
{
Console.WriteLine("");
Console.WriteLine("The following books are " +
"in the {0} category:", p_Category);
while (xPathIt.MoveNext())
{
Console.WriteLine(xPathIt.Current.Value);
}
}
else
{
Console.WriteLine("No books found in the {0} category", p_Category);
}
}
}
}[/code]
<br/>
<
Regards / Freundliche Grüsse King Julien
<br/>
View the full article