asp.net xml object

<Auctions>
  <Auction>
    <FirstName>adsfasd</FirstName>
    <LastName>Yuxcad</LastName>    
    <ID>IiY+D4SIrRpXtEUUu0uy0j1G0LdUJ9bWPj6+ZvO24yE2szPHUFXMfYXirGmWC7asdfasdfsdaO</ID>
    <Status>Pending</Status>
    <Condition>OLD</Condition>
    <Manufacturer>Rickasdfasdy</Manufacturer>
    <CurrencyType>USD</CurrencyType>
    <Bid>
     
     
    </Bid>
  </Auction>
  <Auction>
    <FirstName>adsfasdadfasdf</FirstName>
    <LastName>Yusdf</LastName>    
    <ID>IiY+D4SIrRpXtEUUu0uy0j1G0LdUJ9bWPj6+ZvO24yE2szPHUFXMfYXirGmWC7aO</ID>
    <Status>Pending</Status>
    <Condition>OLD</Condition>
    <Manufacturer>Rickasdfy</Manufacturer>
    <CurrencyType>USD</CurrencyType>
    <Bid>
     
     
    </Bid>
  </Auction>
</Auctions>

I have below xml format. What I would like to do is:

Read/Find the ID = "IiY+D4SIrRpXtEUUu0uy0j1G0LdUJ9bWPj6+ZvO24yE2szPHUFXMfYXirGmWC7aO"
if isFound Add record within the <Bid></Bid>

like below

<Bid>
      <price>100</price>
      <createdDateTime>1:00pm</createdDateTime>
</Bid>
 

How can I do that by using XMLDocument Object in c#?
LVL 1
ITsolutionWizardAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ITsolutionWizardAuthor Commented:
I am half way done. I can search already just do not have to add bid within <bid></bid>
public string Add(DirDomain.Auction a, int price) //add Bid Information
        {
            string s = "No result";
            using (XmlReader reader = XmlReader.Create(DomainCompany.auctionXmlFileDir))
            {
                while (reader.Read())
                {
                    if (reader.IsStartElement())
                    {
                        switch (reader.Name.ToString())
                        {
                            case "ID":
                                string ID = reader.ReadString();
                                if (ID == "IiY+D4SIrRpXtEUUu0uy0j1G0LdUJ9bWPj6+ZvO24yE2szPHUFXMfYXirGmWC7aO")
                                {
                                   #region add bid
                                   //add bid here
                                   #endregion
                                    break;
                                }
                            break;
                        }
                    }
                }
            }
            return s;
        }
Carl TawnSystems and Integration DeveloperCommented:
XmlReader isn't really what you want. As the name suggests, it is for reading an Xml document rather than writing to one.

This sample should be more like what you want:
            XmlDocument doc = new XmlDocument();
            doc.Load(@"C:\temp\temp.xml");

            XmlNode bid = doc.SelectSingleNode("//Auction[ID='IiY+D4SIrRpXtEUUu0uy0j1G0LdUJ9bWPj6+ZvO24yE2szPHUFXMfYXirGmWC7aO']/Bid");
            if (bid != null)
            {
                XmlNode newNode = doc.CreateElement("price");
                newNode.InnerText = "100";
                bid.AppendChild(newNode);

                newNode = doc.CreateElement("createdDateTime");
                newNode.InnerText = "1:00pm";
                bid.AppendChild(newNode);
            }

            doc.Save(@"C:\temp\temp.xml");

Open in new window

Fernando SotoRetiredCommented:
Hi ITsolutionWizard;

Here is a solution using Linq to XML on a XML document.

// ID to search for in the XML document
string ID = "IiY+D4SIrRpXtEUUu0uy0j1G0LdUJ9bWPj6+ZvO24yE2szPHUFXMfYXirGmWC7aO";

// Load the XML document from the file system
XDocument xdoc = XDocument.Load("Directory path and file name xxx.xml");
// If the XML document is in memory as a string then you can use uncomment this next line and comment out the above line.
// XDocument xdoc = XDocument.Parse(StringVariableName);

// Find the Auction you are looking for.
XElement auction = (from node in xdoc.Descendants("Auction")
                    where node.Element("ID").Value.Equals(ID)
                    select node).SingleOrDefault();

// If found Modify it
if( auction != null )
{
    // Auction was found add nodes here to Bid
    auction.Element("Bid").Add(new XElement("price", "100"));
    auction.Element("Bid").Add(new XElement("createdDateTime", "1:00pm"));    
    // Once the above two lines execute the XML document in 
    // variable xdoc will reflect the new nodes as well
}

// If you want to save the XML to the file system execute the next line.
xdoc.Save("Path and file name of the new XML document");

Open in new window

Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

ITsolutionWizardAuthor Commented:
Carl Tawn: You are right. Yours working but now I come up with 1 issue.
The xml structure has some issues.
Now I hope to see something like below.

<Bid>
      <Log>
         <Price>1.00</Price>
         <CreatedDateTime>1:00</CreatedDate>
         <CreatedBy>Me</CreatedBy>
      </Log>
      <Log>
         <Price>2.00</Price>
         <CreatedDateTime>1:00</CreatedDate>
         <CreatedBy>Someone else</CreatedBy>
      </Log>      
    </Bid>

So I will be able to query in the future. Can you help me with the codes you already created?  Thanks
Carl TawnSystems and Integration DeveloperCommented:
Creating an extra level is easy enough. I've expanded the previous sample to include an extra level with the "log" node:
            XmlDocument doc = new XmlDocument();
            doc.Load(@"C:\temp\temp.xml");

            XmlNode bid = doc.SelectSingleNode("//Auction[ID='IiY+D4SIrRpXtEUUu0uy0j1G0LdUJ9bWPj6+ZvO24yE2szPHUFXMfYXirGmWC7aO']/Bid");
            if (bid != null)
            {
                // create Log element
                XmlNode log = doc.CreateElement("Log");

                // create child nodes and add to Log
                XmlNode newNode = doc.CreateElement("price");
                newNode.InnerText = "100";
                log.AppendChild(newNode);

                newNode = doc.CreateElement("createdDateTime");
                newNode.InnerText = "1:00pm";
                log.AppendChild(newNode);

                // add Log node to Bid node
                bid.AppendChild(log);
            }

            doc.Save(@"C:\temp\temp.xml");

Open in new window

ITsolutionWizardAuthor Commented:
It does not work. The log node is created at the end of the xml file . It does not stay within the log
Carl TawnSystems and Integration DeveloperCommented:
It should work fine based on your original XML.  Has the XML structure changed?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ITsolutionWizardAuthor Commented:
You are right. It work. My bad.

I have another one need help.

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_28640165.html
ITsolutionWizardAuthor Commented:
Excellent Helper!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.