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

asked on

C# xml string

I have something like below. And all I need is to get xml string
And then save as new xml

<products>
<product>a</producf>
<product>b</product>
</products>

Return xml file and get just product a.
Avatar of ITsolutionWizard
ITsolutionWizard
Flag of United States of America image

ASKER

Any helps
Please try this -

XDocument xdoc = XDocument.Load(@"C:\Pawan\Pawan.xml");
            var x = from r in xdoc.Descendants("products").Where(r => (string)r.Element("product") == "a")
                    select new { Product = r.Element("product")};
            XElement rt = new XElement("Products", from r in x select r.Product);
            rt.Save(@"C:\Pawan\Pawan_out.xml");

Open in new window


output

<?xml version="1.0" encoding="utf-8"?>
<Products>
  <product>a</product>
</Products>

Open in new window

i do not think your codes are working.
if i have product from a to z. , that will be an issue
You need products with a only right ?
please just assume i will pass out string from a to z. and we will have a to z product in xml
So what you need in output ...???/
ok got, wait.
xml file
 private void CreateXmlfile(string word){
                XDocument xdoc = XDocument.Load(@"C:\Pawan\Pawan.xml");
            var x = from r in xdoc.Descendants("products").Where(r => (string)r.Element("product") == word)
                    select new { Product = r.Element("product")};
            XElement rt = new XElement("Products", from r in x select r.Product);
            rt.Save(@"C:\Pawan\Pawan_out_"+ word +".xml");

}

Open in new window

Try this method and pass different string into it.It will create different xml's based on string passed.
will your codes till wor if I change the xml structure as long as I still have product element?
I think it will work. Let me know if you face any issues.
thank. i have another issue below if you can help

https://www.experts-exchange.com/questions/29061260/mvc-xml.html
i have the codes below and not working



my codes
 
private void CreateXmlfile(string word)
        {
            XDocument xdoc = XDocument.Load(fileCreateQuote);
            var x = from r in xdoc.Descendants("GeneralInformation").Where(r => (string)r.Element("Name") == word)
                    select new { Product = r.Element("Bond") };
            XElement rt = new XElement("Bond", from r in x select r.Product);
            rt.Save(@"D:\Codes\Developer14\Insurance\Insurance\Data\SuretyBond\Quote" + word + ".xml");

        }
      

Open in new window


expect result

<?xml version="1.0" encoding="utf-8" ?>
<Surety>
  <SuretyLine>
    <Commercial>
      <Bond>
        <GeneralInformation UI="BondInfo"> 
          <Name ID="bondName" PrefillValue="Yes">Contractor License Bond</Name>
          <State ID="bondState" PrefillValue="Yes">CA</State>
          <Description>
            This is contractor bond....
          </Description>
        </GeneralInformation>
        <Coverage>
          <Amount>15000</Amount>
        </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>
        <Obligee>
          <Name PrefillValue="Yes">Contractor License Board</Name>
          <StreetName PrefillValue="Yes">9821 Business Park Drive</StreetName>
          <City PrefillValue="Yes">Sacramento</City>
          <State PrefillValue="Yes">CA</State>
          <Zip PrefillValue="Yes">95827</Zip>
          <PhoneNo PrefillValue="Yes">(800)321-2752</PhoneNo>
          <Website PrefillValue="Yes">http://www.cslb.ca.gov</Website>
        </Obligee>
        <Document>
          <Form>FormCA9081.pdf</Form>
          <App>
            <Company01>Company01.pdf</Company01>
          </App>
        </Document>
        <Image></Image>
      </Bond>
     </Commercial>
  </SuretyLine>
</Surety>

Open in new window


oringial:
  private void CreateXmlfile(string word)
        {
            XDocument xdoc = XDocument.Load(fileCreateQuote);
            var x = from r in xdoc.Descendants("GeneralInformation").Where(r => (string)r.Element("Name") == word)
                    select new { Product = r.Element("Bond") };
            XElement rt = new XElement("Bond", from r in x select r.Product);
            rt.Save(@"D:\Codes\Developer14\Insurance\Insurance\Data\SuretyBond\Quote" + word + ".xml");

Open in new window


        }
Any help
Help?
checking.
ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India 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