2

I have created the xml file (infopath form) from the template(xsn) programatically. I have the following xml file structure from the template file(xsn). Now I want to append values to the xml file.

   <my:PropertyDetails>
    <my:AddressSelectionList>2201.00000000000</my:AddressSelectionList>
    <my:PropRef>210</my:PropRef>
    <my:UPRN>2201.00000000000</my:UPRN>
    <my:AddressLine>220 test road</my:AddressLine>
    <my:PropId>210</my:PropId>
    <my:BlockUPRN></my:BlockUPRN>
    <my:Attachments xsi:nil="true"></my:Attachments>
    <my:Filegroup>
      <my:URL></my:URL>
      <my:URLText></my:URLText>
    </my:Filegroup>
  </my:PropertyDetails>
  <my:ScaffoldMeasure>
    <my:groupRepeat>
      <my:Description></my:Description>
      <my:Code></my:Code>
      <my:Unit></my:Unit>
      <my:Rate xsi:nil="true"></my:Rate>
      <my:Quantity></my:Quantity>
      <my:Cost xsi:nil="true"></my:Cost>
      <my:Comments></my:Comments>
      <my:ID></my:ID>
      <my:Title></my:Title>
    </my:groupRepeat>
  </my:ScaffoldMeasure>
<my:PorchBalcony>
    <my:groupRepeat>
      <my:Description></my:Description>
      <my:Code></my:Code>
      <my:Unit></my:Unit>
      <my:Rate xsi:nil="true"></my:Rate>
      <my:Quantity></my:Quantity>
      <my:Cost xsi:nil="true"></my:Cost>
      <my:Comments></my:Comments>
      <my:ID></my:ID>
      <my:Title></my:Title>
    </my:groupRepeat>
  </my:PorchBalcony>

I am trying to access as follows:

> XmlNodeList properNodeList =
> xmlDoc.GetElementsByTagName("my:PropertyDetails");
> XmlNodeList smNodeList = xmlDoc.GetElementsByTagName("my:ScaffoldMeasure");

var a=  new XElement(
                myns + "groupRepeat",
                from c in xmlDoc.Elements(nsm + "ScaffoldMeasure") select 
                new XElement(nsm + "groupRepeat",
       new XElement(nsm + "Description", "scaffold1"),
                new XElement(nsm + "Code", "sc1"),
                new XElement(nsm + "Unit", "sc1"),
                new XElement(nsm + "Rate", "sc1"),
                new XElement(nsm + "Quantity", "sc1"),
                new XElement(nsm + "Cost", "sc1"),
                new XElement(nsm + "Comments", "sc1"),
                new XElement(nsm + "ID", "sc1"),
                new XElement(nsm + "Title", "ssssssss")));         
       xmlDoc.Save(@"C:\ttt.xml");

Now I need to append the values in the groupRepeat section. Can anybody suggest whats the best way to append values to the section?
Thanks

har07
  • 88,338
  • 12
  • 84
  • 137
Jyothi Srinivasa
  • 701
  • 2
  • 9
  • 26

1 Answers1

0

Even after several clarifications I asked in comment, I'm still not sure what you are after exactly. How about this :

.....
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";

var sm = xmlDoc.Descendants(nsm + "ScaffoldMeasure").First();
var groupRepeat = new XElement(nsm + "groupRepeat",
                       new XElement(nsm + "Description", "scaffold1"),
                       new XElement(nsm + "Code", "sc1"),
                       new XElement(nsm + "Unit", "sc1"),
                       new XElement(nsm + "Rate", "sc1", 
                                        new XAttribute(xsi + "nil", true)),
                       new XElement(nsm + "Quantity", "sc1"),
                       new XElement(nsm + "Cost", "sc1", 
                                        new XAttribute(xsi + "nil", true)),
                       new XElement(nsm + "Comments", "sc1"),
                       new XElement(nsm + "ID", "sc1"),
                       new XElement(nsm + "Title", "ssssssss"));
sm.Add(groupRepeat);
xmlDoc.Save(@"C:\ttt.xml");

I assumed you have exactly one <my:ScaffoldMeasure> node in the XML and you want to add another <my:groupRepeat> node completed with child nodes within that <my:ScaffoldMeasure>.

har07
  • 88,338
  • 12
  • 84
  • 137