我有以下xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
<DataSets>
<DataSet Name="Data">
<Query>
<DataSourceName>DS1</DataSourceName>
<CommandText>text</CommandText>
</Query>
</DataSet>
<DataSet Name="table22">
<Query>
<DataSourceName>DS1</DataSourceName>
<CommandText>New text2</CommandText>
</Query>
我必须在第一个&#34; Query&#34;结束后添加以下xml文本。使用Xelement和XAttribute的元素 我要添加的第一个xml文本是:
<Field Name="CommunicationDataValueId">
<DataField>CommunicationDataValueId</DataField>
<TypeName>System.Int64</TypeName>
</Field>
<Field Name="DeviceMasterId">
<DataField>DeviceMasterId</DataField>
<TypeName>System.Int32</TypeName>
</Field>
我在第二个查询元素结束后要添加的第二个xml文本:
<Field Name="Min">
<DataField>Min</DataField>
<rd:TypeName>System.String</rd:TypeName>
</Field>
<Field Name="Max">
<DataField>Max</DataField>
<rd:TypeName>System.Int64</rd:TypeName>
</Field>
怎么做???
预期输出应为:
<?xml version="1.0" encoding="UTF-8"?>
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
<DataSets>
<DataSet Name="Data">
<Query>
<DataSourceName>DS1</DataSourceName>
<CommandText>text</CommandText>
</Query>
<Field Name="CommunicationDataValueId">
<DataField>CommunicationDataValueId</DataField>
<TypeName>System.Int64</TypeName>
</Field>
<Field Name="DeviceMasterId">
<DataField>DeviceMasterId</DataField>
<TypeName>System.Int32</TypeName>
</Field>
</DataSet>
<DataSet Name="table22">
<Query>
<DataSourceName>DS1</DataSourceName>
<CommandText>New text2</CommandText>
</Query>
<Field Name="Min">
<DataField>Min</DataField>
<rd:TypeName>System.String</rd:TypeName>
</Field>
<Field Name="Max">
<DataField>Max</DataField>
<rd:TypeName>System.Int64</rd:TypeName>
</Field>
任何人都可以帮忙解决这个问题!!!!!!!
答案 0 :(得分:1)
使用XDocument
var doc = XDocument.Parse("your xml string");
//namespace
var nspace = doc.Root.Name.Namespace;
foreach (var item in doc.Descendants(nspace+"DataSet"))
{
var str = new StringBuilder();
var str1 = new StringBuilder();
str.Append("<Field Name='CommunicationDataValueId'>");
str.Append("<DataField>CommunicationDataValueId</DataField>");
str.Append("<TypeName>System.Int64</TypeName>");
str.Append("</Field>");
str1.Append("<Field Name='DeviceMasterId'>");
str1.Append("<DataField>DeviceMasterId</DataField>");
str1.Append("<TypeName>System.Int32</TypeName>");
str1.Append("</Field>");
item.Add(XElement.Parse(str.ToString()));
item.Add(XElement.Parse(str1.ToString()));
}
答案 1 :(得分:0)
请查看XElement - AddAfterSelf
的以下API。
您可以找到每个相关的Query
元素并调用该方法,添加您要添加的XML片段。