-
December 20th, 2013, 06:59 AM
#1
All attribute values Printed the Same.
Hello.
Why is it?
I want attributes to have different values.
Here is my code:
Code:
objNodeList = doc.SelectNodes( strXpath );
...
for(int i=0;i<objNodeList.Count;++i)
{
Console.WriteLine(
objNodeList.Item(2).Attributes.Item(0).Value
);
}
In case you like to see my xml file, here is a part of it:
Code:
<Cars><Car Totalvalue="120000"><CarDetail><Name>Pride</Name><Model>Sedan</Model><ItemsInStock>20</ItemsInStock><Price>6000</Price><Description><Color>Orange</Color><EngSize>1300</EngSize></Description></CarDetail></Car><Car Totalvalue="120000"><CarDetail><Name>Samand</Name><Model>Sedan</Model><ItemsInStock>15</ItemsInStock><Price>8000</Price><Description><Color>White</Color><EngSize>1800</EngSize></Description></CarDetail></Car><Car Totalvalue="420000"><CarDetail><Name>Dodge-Dart</Name><Model>Sedan</Model><ItemsInStock>30</ItemsInStock><Price>14000</Price><Description><Color>red</Color><EngSize>3000</EngSize></Description></CarDetail></Car>
-
December 20th, 2013, 11:53 AM
#2
Re: All attribute values Printed the Same.
Imo, manually traversing the xmldocument is the old way of doing it. Instead wouldn't you want to change your data on a class level basis where you modify properties and insert/remove items from a list? You may wonder how this is possible since you are dealing with xml. The answer is XmlSerialization. See my response in the following thread for an explanation and example code for using XmlSerialization to read and write xml from .Net.
How to Parse XML String
-
December 20th, 2013, 12:04 PM
#3
Re: All attribute values Printed the Same.
Btw, is reformatting your xml an option? In other words, change you change the format are you getting it from somewhere else and can't change the format?
-
December 20th, 2013, 12:50 PM
#4
Re: All attribute values Printed the Same.
Originally Posted by Arjay
Imo, manually traversing the xmldocument is the old way of doing it. Instead wouldn't you want to change your data on a class level basis where you modify properties and insert/remove items from a list? You may wonder how this is possible since you are dealing with xml. The answer is XmlSerialization. See my response in the following thread for an explanation and example code for using XmlSerialization to read and write xml from .Net.
How to Parse XML String
Dear sir
I should first of all thank you for your kind response.
I am a student and I have to do it using XmlDocument. so XmlReader, Xmlwriter (asothers pointed out) or your XmlSerialization are not an option for this exercise.
I have coded and studied C++ for quite some time, But I am new to C#. So it's no wonder that my coded looks elementary.
-
December 20th, 2013, 01:01 PM
#5
Re: All attribute values Printed the Same.
Originally Posted by Arjay
Btw, is reformatting your xml an option? In other words, change you change the format are you getting it from somewhere else and can't change the format?
It had a nice format at the beginning.But I changed it to add an attribute to the node <Car> .It looked this way at the beginning:
<Car>
<CarDetail>
<Name>Pride</Name>
<Model>Sedan</Model>
<ItemsInStock>20</ItemsInStock>
<Price>6000</Price>
<Description>
<Color>Orange</Color>
<EngSize>1300</EngSize>
</Description>
</CarDetail>
</Car>
This is how I managed to change it!
XmlWriter objXmlWriter = XmlTextWriter.Create( strOutFile );
// XmlWriter writer2 = XmlTextWriter.Create( str2 );
XmlDocument doc = new XmlDocument();
doc.Load( strPath );
doc2.Load( strPath );
strXpath = "//Car";
XmlNodeList objNodeList;
XmlAttribute newAttr = doc.CreateAttribute("Totalvalue");
objNodeList = doc.SelectNodes( strXpath );
XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;
objXmlWriter.WriteStartElement("Cars");
for(int i=0;i<objNodeList.Count;++i)
{
newAttr.Value = Convert.ToString(
int.Parse(objNodeList[i].FirstChild.ChildNodes[3].InnerXml)
* int.Parse(objNodeList[i].FirstChild.ChildNodes[2].InnerXml));
attrColl.Append(newAttr);
objNodeList[i].Attributes.Prepend(newAttr);
objNodeList[i].WriteTo(objXmlWriter);
}
objXmlWriter.WriteEndDocument();
objXmlWriter.Close();
I know that may not have been a very good way(especially considering the ugly look of the produced file and the overhead of creating collections), but I hope it will earn me some points.
-
December 20th, 2013, 02:23 PM
#6
Re: All attribute values Printed the Same.
I first tried to apply Insersion sort on the collection. Failing that(loop variables seem read-only), I tried to copy the most valuable node, then the 2nd most valuable node, then the third, etc, to an array and then outputting it to another file.
Am not sure if there is any other solution
-
December 20th, 2013, 04:18 PM
#7
Re: All attribute values Printed the Same.
Probably the best thing you could do at the moment is to put your code and assignment aside for a minute and work through a couple of online C# XmlDocument tutorials. Search bing or google for "C# xmldocument tutorial". There looks like a couple of good ones in the first few links.
After you take the time to work through a couple of how to read and write using xmldocument tutorials, it will be easy to go back your code and figure out what you are doing wrong and/or rewrite the code.
-
December 20th, 2013, 10:40 PM
#8
Re: All attribute values Printed the Same.
I know why this result is obtained.
each time I was choosing 'Item(2)' of the collection.
I changed it to 'Item(i)'.
but then again my code just finds the most valuable node and puts it in the array. I tried to delete it in the iteration after each addition, but 'null reference' error occurs.
I have other lessons too, this semester, and this lesson i not just about xmlDocument.It has another part too.
Last edited by soheil2; December 20th, 2013 at 10:42 PM.
-
December 20th, 2013, 11:30 PM
#9
Re: All attribute values Printed the Same.
Well, go back to the first part and actually try to LEARN it. Quicker than asking in the long run
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|