CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2013
    Posts
    26

    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>

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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?

  4. #4
    Join Date
    Dec 2013
    Posts
    26

    Re: All attribute values Printed the Same.

    Quote Originally Posted by Arjay View Post
    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.

  5. #5
    Join Date
    Dec 2013
    Posts
    26

    Re: All attribute values Printed the Same.

    Quote Originally Posted by Arjay View Post
    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.

  6. #6
    Join Date
    Dec 2013
    Posts
    26

    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

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  8. #8
    Join Date
    Dec 2013
    Posts
    26

    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured