ITsolutionWizard
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</Fi rstName>?
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 ").FirstOr Default()
Let me know if this is possible.
how can we add
'John' into <FirstName ID="applicantFirstName" PrefillValue="No">John</Fi
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
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>
ASKER
It won't work. I need to do wild search...remember assume I don't know the xml structure
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.
ASKER
But how to save the xml with the values?
ASKER
And there is attribute called prefillvalue
If no, it means it need to fill the value
If yes, no need to do anything
If no, it means it need to fill the value
If yes, no need to do anything
ASKER
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");
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.
ASKER
i do not understand what you mean...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
XMLdocument to read the entire document in one shot and search that for your tags.
Open in new window