Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

linq, xml, complex, c#

I have xml below, and If i just give you  'applicantFirstName' and the value is 'John'
how can we add
'John' into       <FirstName ID="applicantFirstName" PrefillValue="No">John</FirstName>?

based on two requirements

1. I will only pass you  Dictionary<string, string> e.g. <applicantFirstName,'john'>
2. You actually do not know the structure of xml except you know each element has "ID" attritbute. meaning that you pass 'applicantFfirstName'' only into xml, and try to find element, and add value (John) there.

so you won't be able to use linq like doc.Descendants("Applicant").FirstOrDefault()

Let me know if this is possible.

<Surety>
  <SuretyLine>
    <Commercial>
      <Bond>
        <GeneralInformation UI="BondInfo">
          <Name ID="bondName" PrefillValue="Yes">Contractor</Name>
          <State ID="bondState" PrefillValue="Yes">CA</State>
          
          <Code PrefillValue="Yes">888</Code>
          <Renew PrefillValue="Yes">
            <Year>1</Year>
          </Renew>
        </GeneralInformation>
        <Coverage>
          <Amount PrefillValue="Yes">15000</Amount>
          <Year PrefillValue="Yes">1</Year>
        </Coverage>
        <Applicant>
          <CompanyName ID="applicantCompanyName" PrefillValue="No"></CompanyName>
          <CompanyEntityType ID="applicantCompanyEntityType" PrefillValue="No"></CompanyEntityType>
          <CompanyTaxID ID="applicantCompanyTaxID"></CompanyTaxID>
          <FirstName ID="applicantFirstName" PrefillValue="No"></FirstName>
          <LastName ID="applicantLastName" PrefillValue="No"></LastName>
          <MiddleName ID="applicantMiddleName" PrefillValue="No"></MiddleName>
          <StreetName ID="applicantStreetName" PrefillValue="No"></StreetName>
          <City ID="applicantCity" PrefillValue="No"></City>
          <State ID="applicantState" PrefillValue="No"></State>
          <Zip ID="applicantZip" PrefillValue="No"></Zip>
          <PhoneNo ID="applicantPhoneNo" PrefillValue="No"></PhoneNo>
          <FaxNo ID="applicantFaxNo" PrefillValue="No"></FaxNo>
          <Email ID="applicantEmail" PrefillValue="No"></Email>
          <Ssn ID="applicantSSN" PrefillValue="No"></Ssn>
          <DriverLicense ID="applicantDriverLicense" PrefillValue="No"></DriverLicense>
          <DriverLicenseSate ID="applicantDriverLicenseState" PrefillValue="No"></DriverLicenseSate>
        </Applicant>
        <AddtionalInformation>
          <div class="col-lg-12">
            <Input Type="text" Name="licenseNo" ID="additionalInfo_licenseNo" Onchange="fnSaveSingleData('licenseNo','1')" Placeholder="Enter License No." Class="form-control" Title="License is required!" PrefillValue="No" Required="" />
          </div>
        </AddtionalInformation>
        
       
        <Image></Image>
        <Status>Online</Status>
      </Bond>
    </Commercial>
  </SuretyLine>
</Surety>

Open in new window

Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

you can do something like below

XMLdocument to read the entire document in one shot and search that for your tags.

using System.Xml;
XmlDocument document = new XmlDocument();
            document.Load("C:\\Pawan\\pawan1.xml");

            foreach (XmlNode node in document.GetElementsByTagName("FirstName"))
            {
                ...if ( node.value == "John" )
            }

Open in new window

Avatar of ITsolutionWizard

ASKER

It won't work. I need to do wild search...remember assume I don't know the xml structure
Avatar of Fernando Soto
This should do what you need
// Test Data
// Find Attribute from key and update value of node with Value 
Dictionary<string, string> update = new Dictionary<string, string>();
update.Add("applicantFirstName", "John");

// Load the XML document from the file system
XDocument doc = XDocument.Load(@"C:\Working Directory\Surety.xml");

foreach (KeyValuePair<string, string> element in update)
{
    // Find node to update
    var node = doc.Descendants().Where(a => 
        a.HasAttributes && 
        a.Attribute("ID") != null &&
        a.Attribute("ID").Value == element.Key
        ).FirstOrDefault();
    // If node is not null update the node in XML document
    if (node != null)
    {
        node.Value = element.Value;
    }
}

// At this point the XML document, doc, has been updated.

Open in new window

But how to save the xml with the values?
And there is attribute called prefillvalue
If no, it means it need to fill the value
If yes, no need to do anything
Sorry forget to mention it
To your question, "But how to save the xml with the values? ", after line 22 in my last post do the following
doc.Save("FilePathAndNameHere");

Open in new window

To your question, "And there is attribute called prefillvalue...", In the if statement you will have the node you will be modifying just test that node for the Attribute and its value and do what you need to do.
i do not understand what you mean...
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial