質問

i have following xml file :

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

i have to add following xml text after ending of first "Query" element using Xelement and XAttribute my first xml text to add is:

   <Field Name="CommunicationDataValueId">
    <DataField>CommunicationDataValueId</DataField>
    <TypeName>System.Int64</TypeName>
    </Field>
    <Field Name="DeviceMasterId">
    <DataField>DeviceMasterId</DataField>
    <TypeName>System.Int32</TypeName>
    </Field>

my second xml text to add after ending of second query element :

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

How to do it???

Expected output should be:

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

can anyone help to solve this please!!!!!!!

役に立ちましたか?

解決

By using 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()));
    }

他のヒント

Take a look at the following API for XElement - AddAfterSelf.

You can find each relevant Query element and call that method, adding the piece of XML you want to add.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top