|
Hello, i would like to know how to sort an xml file. here is the xml file i have:
<?xml version='1.0'?>
<Books>
<Book>
<Title>Advanced XML</Title>
<Publisher>Lucerne Publishing</Publisher>
</Book>
<Book>
<Title>Learn XML Today</Title>
<Publisher>MSPress</Publisher>
</Book>
<Book>
<Title>XML for Gurus</Title>
<Publisher>Lucerne Publishing</Publisher>
</Book>
<Book>
<Title>Developing XML solutions</Title>
<Publisher>MSPress</Publisher>
</Book>
</Books>
I don't have any template or style file.
I want to display a sorted information inside a listbox in C#:
Advanced XML, Lucerne Publishing
XML for Gurus, Lucerne Publishing
Developing XML solutions, MSPress
Learn XML Today, MSPress
I assume that we want to sort by the publisher first and then by the title.
Any suggestion will be great.
Donkaiser
|
|
|
|
|
private static void SortXmlTest()
{
string data = @"<?xml version='1.0'?>
<Books>
<Book>
<Title>Advanced XML</Title>
<Publisher>Lucerne Publishing</Publisher>
</Book>
<Book>
<Title>Learn XML Today</Title>
<Publisher>MSPress</Publisher>
</Book>
<Book>
<Title>XML for Gurus</Title>
<Publisher>Lucerne Publishing</Publisher>
</Book>
<Book>
<Title>Developing XML solutions</Title>
<Publisher>MSPress</Publisher>
</Book>
</Books>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(data);
ArrayList books = new ArrayList();
foreach (XmlElement book in doc.DocumentElement.ChildNodes)
books.Add(book);
books.Sort(new BookComparer());
doc.DocumentElement.RemoveAll();
foreach (XmlElement book in books)
doc.DocumentElement.AppendChild(book);
doc.Save(Console.Out);
}
class BookComparer : IComparer
{
public int Compare(object x, object y)
{
//return (x as XmlElement)["Title"].InnerText.CompareTo((y as XmlElement)["Title"].InnerText);
int publisherCompare = (x as XmlElement)["Publisher"].InnerText.CompareTo((y as XmlElement)["Publisher"].InnerText);
if (publisherCompare != 0)
return publisherCompare;
return (x as XmlElement)["Title"].InnerText.CompareTo((y as XmlElement)["Title"].InnerText);
}
}
I hope I didn't just do all your homework.
|
|
|
|
|
Hi Reinux,
Thank you for your help. im still new to XML programming.
I was compiling ur code and it gave me this error in vs 2005:
"Using the generic type 'System.Collections.Generic.IComparer<t>' requires '1' type arguments"
I've been looking around and i didn't see any implementation of the parameter.
Thx again
Donkaiser
|
|
|
|
|
Ah, that was actually .NET 1.1 code. You can still make it work by adding
"using System.Collections" to the top of your code, but here's the .NET 2.0 version (you don't need BookComparer):
private static void SortXmlTest()
{
string data = @"<?xml version='1.0'?>
<Books>
<Book>
<Title>Advanced XML</Title>
<Publisher>Lucerne Publishing</Publisher>
</Book>
<Book>
<Title>Learn XML Today</Title>
<Publisher>MSPress</Publisher>
</Book>
<Book>
<Title>XML for Gurus</Title>
<Publisher>Lucerne Publishing</Publisher>
</Book>
<Book>
<Title>Developing XML solutions</Title>
<Publisher>MSPress</Publisher>
</Book>
</Books>";
//Create a new document and load the sample data.
XmlDocument doc = new XmlDocument();
doc.LoadXml(data);
//Copy all of the Book elements to a list.
List<XmlElement> books = new List<XmlElement>();
foreach (XmlElement book in doc.DocumentElement.ChildNodes)
books.Add(book);
//Sort the data -- if the publishers are the same, sort by title.
books.Sort(delegate(XmlElement x, XmlElement y)
{
int publisherCompare = x["Publisher"].InnerText.CompareTo(y["Publisher"].InnerText);
if (publisherCompare != 0)
return publisherCompare;
return x["Title"].InnerText.CompareTo(y["Title"].InnerText);
});
//Remove all the Book elements from the document, and put the sorted copy back in.
doc.DocumentElement.RemoveAll();
foreach (XmlElement book in books)
doc.DocumentElement.AppendChild(book);
//Print the data to the console.
doc.Save(Console.Out);
}
|
|
|
|
|
uraaa! I got it working and listed on a textbox. If i listed only the titles of the books in the listbox. when i select an item an click an "ok button". How can a retrieve the matching publisher?
Thanks a lot again
Donkaiser
|
|
|
|
|
Practically every control in Windows Forms has a handy Tag property that you can assign what ever you want to.
So you can assign those book XmlElement items that you extracted earlier to each one of their corresponding list items.
And then when you click an ok button, check the listbox.SelectedItem.Tag property -- that'll be whatever you assigned to the tag.
Edit: sorry, that's ListView I'm thinking of.
ListBox lets you assign any type of object, so you can make a custom class and override the ToString method to have it show the right text in the list box.
So make a class like this:
class Book
{
public Book(string name, string publisher)
{
this.name = name;
this.publisher = publisher;
}
string name, publisher;
public string Publisher
{
get { return publisher; }
}
public string Name
{
get { return name; }
}
public override string ToString()
{
//Just show the name
return name;
}
}
When you add an item to the list box, do something like this:
listBox.Add(new Book(element["Name"], element["Publisher"]);
When you want to get the publisher for the selected book, do this:
publisher = (listBox.SelectedItem as Book).Publisher;
-- modified at 19:59 Friday 19th May, 2006
|
|
|
|
|
I am writing some code in C# and have the need to call a third party java api. I am attempting to write a C++ dll that can be called from
C# -> C++ -> Java -> Java API.
Everytime I make a call to JNI_CreateJavaVM from Java I get a JNI_ERR or -1
I do have a java class file in the same directory.
I also have the location of jvm.dll in the system path variable.
Also, if I run the C++ code as a stand-alone executable instead of a DLL it works.
Any Help would be appreciated.
Thanks
JNIEnv *env;
JavaVM *jvm;
jint res;
JavaVMInitArgs vm_args;
avaVMOption options[1];
options[0].optionString = "-Djava.class.path=C: \\CsstProductInterface.jar";
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = JNI_TRUE;
res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
if(res < 0)
{
AddToLog("UNABLE to start JVM\n\n");
}
Pat
|
|
|
|
|
I have a piece of code in which collection is assigned to dropdown list's data source property for binding data. When multiple clients access the site simultaniously the following exception was being thrown:"Collection was modified; Enumeration Operation may not execute". Any idea about y this is happening and how to get this code work???
|
|
|
|
|
Is not allowed to modify any item of a collection in any way while you're inside the foreach loop on that collection.
You may try to lock the piece of code, something like the following
lock(this) {
...
}
paco
|
|
|
|
|
Thank you carlopagliei for the reply; but i am not using the foreach statement. I have a code like
ddlTest.DataSource= TestCollection;
ddlTest.DataBind();
Otherthan populating the collection from database, i am not manipulating the collection.
|
|
|
|
|
It was only an example. You don't use foreach but probably the problem is that while DataBind() method enumerates over the TestCollection another piece of code (maybe the same but from another thread) is doing the same. So try to lock the code that acess TestCollection before use it:
lock (TestCollection) {
ddlTest.DataSource= TestCollection;
ddlTest.DataBind();
}
paco
|
|
|
|
|
Hi.
when we create a class, and in code type 'ClassName.', some methods and properties appear, and on an object of that class there is another properties and methods.
first : we don't write them for that class, these properties and methods come from where?
second : for class and object these are difference, why?(Is it for static methods and properties?)
Best wishes
|
|
|
|
|
mehrdadc48 wrote: first : we don't write them for that class, these properties and methods come from where?
Are you referring to methods such as ToString()? They would be inherited from the base class (in this case, System.Object).
mehrdadc48 wrote: second : for class and object these are difference, why?(Is it for static methods and properties?)
You are correct. You cannot call static methods using an object instance.
Cheers,
Vikram.
I don't know and you don't either.
Militant Agnostic
|
|
|
|
|
Typing "Classname." invokes intellisense which lists the properties and methods of that class or object so as to ease the work of a prorgammer so he/she does not have to remember all the properties and methods of the Dot Net Framework. There are many classes built into the Dot Net Framework, many of which wrap commonly used functionality that already exists in Windows. In summary, these classes and intellisense are there to make a programmer more productive and are part of the Dot Net Framework installed on your computer.
As for the difference between the methods displayed by Classes and Objects; Yes you are right that for Classes, only static methods are displayed unlike Objects.
|
|
|
|
|
Hi. I have a rss, and I want to remove the items that its pubDate are lower than a DateTime. The problem is that I can't use the function to compare date in XPath because the pubDate use the RFC-822 format and the function in XML/XSD/XSLT, use ISO-8601 format. So I have to use an external function to remove the nodes like this:
public static XmlDocument XMLFilter(XmlDocument doc, DateTime aDate)
XPathNavigator nav = doc.CreateNavigator();
DateTime nodeDate;
foreach (XPathNavigator node in nav.Select("rss/channel/item/pubDate")) {
nodeDate = DateTime.Parse(node.Value).ToLocalTime();
if (nodeDate.CompareTo(aDate.ToLocalTime()) < 0) {
node.DeleteSelf();
}
}
return doc;
}
But If I return the doc, the element are not removed. I want to remove that node (where I compare the condition node.DeleteSelf())
Thanks a lot !!!!!
|
|
|
|
|
Heyyy, I found the way !!! . Thanks anyway people !! May be this solution could help somebody, like me !!
public static XmlDocument XMLFilter(XmlDocument doc, DateTime aDate)
{
DateTime nodeDate;
XmlNode nodeChannel = doc.SelectSingleNode("rss/channel");
XmlNodeList pudDateNodeList = doc.SelectNodes("rss/channel/item/pubDate");
foreach (XmlNode node in pudDateNodeList){
nodeDate = DateTime.Parse(node.InnerText).ToLocalTime();
if (nodeDate.CompareTo(aDate.ToLocalTime()) < 0) {
nodoChannel.RemoveChild(node.ParentNode);
}
}
return documento;
}
|
|
|
|
|
Hi.
I use Flash player component in my project.
How can I close right click on Flashes that it plays.
It don't have click event.
Best wishes
|
|
|
|
|
Hi.
When take right click on solution name in solution explorer, a choice is 'build solution', and another is 'Rebuild solution'.
What is the difference between them ?
Best wishes
|
|
|
|
|
Build just works on modified files, Rebuild deletes all object files etc, and does everything. I think.
Regards,
Rob Philpott.
|
|
|
|
|
Exactly. Build only builds the projects that has files that has changed since last build. Rebuild makes a complete build of the entire solution.
---
b { font-weight: normal; }
|
|
|
|
|
Guffa wrote: Build only builds the projects that has files that has changed
And one has to be careful with this. For example Visual Studio won't notice any changes made to resx files and won't build it until explicitely rebuild has been clicked.
|
|
|
|
|
Hi
How i can drow on web pleas give some exmple.
than'k
|
|
|
|
|
Be more specific, on web pages may be JS help.
------------------------------ "The Soapbox has been so ..."
|
|
|
|
|
If you want the user to be able to draw on a webpage and you save it, the only technologies I can think of which may let you do this are:
Applets (using Java)
and
Flash
Kind regards - Jakob
Kind regards - Jakob
*********************************************
Three kinds of people in the world:
- Those who can count..
- Those who can't!
10 kinds of people in the world:
- Those who understand binary
- Those who don't
|
|
|
|
|
Ok but i don't know how to insert some flash movie on my web please help me with that.
|
|
|
|