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

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,

<?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>

Open in new window

Avatar of ITsolutionWizard
ITsolutionWizard
Flag of United States of America image

ASKER

Any helps
here:

	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!");
		}
	}

Open in new window


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>

Open in new window

or use below @ Line 15

Console.WriteLine(String.Concat(opt.Nodes().Select(x => x.ToString()).ToArray()));

Open in new window


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>

Open in new window

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
Avatar of HainKurt
HainKurt
Flag of Canada 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