从infopath模板创建表单

时间:2014-07-18 08:15:37

标签: c# xml xpath sharepoint-2010 infopath2010

我已经以编程方式从模板(xsn)创建了xml文件(infopath表单)。 我从模板文件(xsn)中获得以下xml文件结构。现在我想将值附加到xml文件。

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

我想按如下方式访问:

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

现在我需要在groupRepeat部分中附加值。任何人都可以建议最新的方法是将值附加到该部分吗? 感谢

1 个答案:

答案 0 :(得分:0)

即使经过多次澄清,我在评论中提到,我仍然不确定完全之后你是什么。怎么样:

.....
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");

我假设您在XML中只有一个<my:ScaffoldMeasure>节点,并且您希望在<my:groupRepeat>内添加另一个已完成子节点的<my:ScaffoldMeasure>节点。