我有一个以下的xml文件:
<Query>
<DataSourceName>DS1</DataSourceName>
<CommandText>SELECT FirstName,Lastname FROM Person</CommandText>
</Query>
<Fields>
<Field Name="FirstName">
<DataField>FirstName</DataField>
<rd:TypeName>System.String</rd:TypeName>
</Field>
<Field Name="Lastname">
<DataField>Lastname</DataField>
<rd:TypeName>System.String</rd:TypeName>
</Field>
</Fields>
我要做的就是,我必须在节点“查询”之后将节点“字段”中的xml文本写入“/ fields”..怎么做... plz帮帮我
答案 0 :(得分:0)
您可以使用Xdocument来管理xml。您可以参考以下代码段将元素添加到标记中。希望这有帮助
XDocument xdoc = XDocument.Load(test.xml");
XNamespace ns = "schemas.microsoft.com/sqlserver/reporting/2005/01";
XNamespace ns1 = "schemas.microsoft.com/SQLServer/reporting";
xdoc.Root.Element(ns + "Fields").Add(new XElement("Field", new XAttribute("Name","ReportId"), new XElement(ns1 + "TypeName", "System.Int64"), new XElement("DataField", "ReportId")));
xdoc.Save(test.xml");
添加命名空间在这里很重要。我附上了源文件和结果文件供您参考。
源文件:
<?xml version="1.0" encoding="utf-8"?>
<Report xmlns="schemas.microsoft.com/sqlserver/reporting/2005/01" xmlns:rd="schemas.microsoft.com/SQLServer/reporting">
<Query>
<DataSourceName>DS1</DataSourceName>
<CommandText>SELECT FirstName,Lastname FROM Person</CommandText>
</Query>
<Fields>
<Field Name="FirstName">
<DataField>FirstName</DataField>
<rd:TypeName>System.String</rd:TypeName>
</Field>
<Field Name="Lastname">
<DataField>Lastname</DataField>
<rd:TypeName>System.String</rd:TypeName>
</Field>
</Fields>
</Report>
结果文件:
<?xml version="1.0" encoding="utf-8"?>
<Report xmlns="schemas.microsoft.com/sqlserver/reporting/2005/01" xmlns:rd="schemas.microsoft.com/SQLServer/reporting">
<Query>
<DataSourceName>DS1</DataSourceName>
<CommandText>SELECT FirstName,Lastname FROM Person</CommandText>
</Query>
<Fields>
<Field Name="FirstName">
<DataField>FirstName</DataField>
<rd:TypeName>System.String</rd:TypeName>
</Field>
<Field Name="Lastname">
<DataField>Lastname</DataField>
<rd:TypeName>System.String</rd:TypeName>
</Field>
<Field Name="ReportId" xmlns="">
<rd:TypeName>System.Int64</rd:TypeName>
<DataField>ReportId</DataField>
</Field>
</Fields>
</Report>