我已经从数据集创建了一个xml文件,但第一个节点是<NewDataset>
我需要将其更改为<FormData>
,我还需要添加一些参数,如Platform =“Android”和版本= “488”。
有什么方法可以在它仍然是数据集时更改它,还是我必须在更改后调用文件然后保存它?
我对数据文件的了解很少,我真的需要一些帮助。
当前的xml文件
<NewDataSet>
<FieldData>
<property_details_gps_location>-29.77861, 31.008617</property_details_gps_location>
<property_details_address_address1>27 MANJEE</property_details_address_address1>
<property_details_address_address2>KENVILLE</property_details_address_address2>
<property_details_address_city>ETHEKWINI</property_details_address_city>
<property_details_address_state>KWAZULU NATAL</property_details_address_state>
</FieldData>
</NewDataSet>
我希望能够实现的目标:
<FormData Platform="Android" PlatformVersion="73" Version="488" DataVersion="1" Description="Investec - Res" FormId="d617a5e8-b49b-4640-9734-bc7a2bf05691" FileId="bce3a788-6725-4ce2-b965-1b55c6e7cc95" EncryptionVerification="" CreatedBy="Shaunm" EditedBy="Shaunm">
<FieldData>
<property_details_gps_location>-29.77861, 31.008617</property_details_gps_location>
<property_details_address_address1>27 MANJEE</property_details_address_address1>
<property_details_address_address2>KENVILLE</property_details_address_address2>
<property_details_address_city>ETHEKWINI</property_details_address_city>
<property_details_address_state>KWAZULU NATAL</property_details_address_state>
</FieldData>
</FormData>
答案 0 :(得分:3)
您可以通过LINQ to XML轻松修改/添加元素/属性。
XDocument doc = XDocument.Parse(dataSetObject.GetXml());
doc.Root.Name = "FormData ";
doc.Root.Add(new XAttribute("Platform", "Android"));
...
doc.Save("sample.xml");
列出子节点
foreach (XElement element in doc.Root.Element("FieldData").Descendants())
{
Console.WriteLine(element.Name + " : " + element.Value );
}
答案 1 :(得分:0)
您可以使用XSL-T对原始流进行简单转换。一旦你拥有它,你可以在内存中编写或操作它。