ITsolutionWizard
asked on
mvc, xml filter
I have xml sample below.
and what I try to do is following:
1. Find State="CA" and name="Contractor"
2. If found, then List Option. In this case, it will have 2 <Option>
3. Inside of the <Option> I try to capture the whole row <Input Type="text" ID="licenseNo" Required="yes"></Input> e.g. and show in cshtml page. How can I do that?
I have c#/mvc application.
Thanks,
and what I try to do is following:
1. Find State="CA" and name="Contractor"
2. If found, then List Option. In this case, it will have 2 <Option>
3. Inside of the <Option> I try to capture the whole row <Input Type="text" ID="licenseNo" Required="yes"></Input> e.g. and show in cshtml page. How can I do that?
I have c#/mvc application.
Thanks,
<?xml version="1.0" encoding="utf-8" ?>
<Surety>
<SuretyLine>
<Commercial>
<Bond>
<GeneralInformation UI="BondInfo">
<Name ID="bondName" prefillValue="Yes">Contractor</Name>
<State ID="bondState" prefillValue="Yes">CA</State>
</GeneralInformation>
<AddtionalInformation>
<Option>
<Label>Please enter license no.</Label>
<Input Type="text" ID="licenseNo" Required="yes"></Input>
</Option>
<Option>
<Label>Please enter license no.</Label>
<Input Type="textarea" ID="licenseNo2" Required="yes"></Input>
</Option>
</AddtionalInformation>
</Bond>
<Bond>
<GeneralInformation UI="BondInfo">
<Name ID="bondName" prefillValue="Yes">Contractor</Name>
<State ID="bondState" prefillValue="Yes">TX</State>
</GeneralInformation>
<AddtionalInformation>
<Option>
<Label>Please enter license no.</Label>
<Input Type="text" ID="licenseNo" Required="yes"></Input>
</Option>
</AddtionalInformation>
</Bond>
</Commercial>
</SuretyLine>
</Surety>
here:
result:
public static void EE29061284()
{
XDocument xdoc = XDocument.Parse("<?xml version=\"1.0\" encoding=\"utf-8\" ?><Surety><SuretyLine><Commercial><Bond><GeneralInformation UI=\"BondInfo\"> <Name ID=\"bondName\" prefillValue=\"Yes\">Contractor</Name><State ID=\"bondState\" prefillValue=\"Yes\">CA</State></GeneralInformation> <AddtionalInformation><Option><Label>Please enter license no.</Label><Input Type=\"text\" ID=\"licenseNo\" Required=\"yes\"></Input></Option><Option><Label>Please enter license no.</Label><Input Type=\"textarea\" ID=\"licenseNo2\" Required=\"yes\"></Input></Option></AddtionalInformation> </Bond><Bond><GeneralInformation UI=\"BondInfo\"><Name ID=\"bondName\" prefillValue=\"Yes\">Contractor</Name><State ID=\"bondState\" prefillValue=\"Yes\">TX</State></GeneralInformation><AddtionalInformation><Option><Label>Please enter license no.</Label><Input Type=\"text\" ID=\"licenseNo\" Required=\"yes\"></Input></Option></AddtionalInformation></Bond></Commercial></SuretyLine></Surety>");
//Find State="CA" and name="Contractor"
XElement bond = (
from b in xdoc.Descendants("Bond")where b.Element("GeneralInformation").Element("State").Value == "CA" && b.Element("GeneralInformation").Element("Name").Value == "Contractor"
select b).FirstOrDefault();
if (bond != null)
{
Console.WriteLine("Found a bond!");
foreach (XElement opt in bond.Elements("AddtionalInformation").Elements("Option"))
{
Console.WriteLine(opt.ToString());
}
}
else
{
Console.WriteLine("Not found!");
}
}
result:
<Option>
<Label>Please enter license no.</Label>
<Input Type="text" ID="licenseNo" Required="yes"></Input>
</Option>
<Option>
<Label>Please enter license no.</Label>
<Input Type="textarea" ID="licenseNo2" Required="yes"></Input>
</Option>
or use below @ Line 15
to get
Console.WriteLine(String.Concat(opt.Nodes().Select(x => x.ToString()).ToArray()));
to get
<Label>Please enter license no.</Label><Input Type="text" ID="licenseNo" Required="yes"></Input>
<Label>Please enter license no.</Label><Input Type="textarea" ID="licenseNo2" Required="yes"></Input>
ASKER
The xml will keep changing in daily basis so using parse with xml string is not working to me. Any option to just load the xml?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER