Merge XElement into XDocument and resolve namespaces

后端 未结 1 1360
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 17:06

Given the following XDocument, initialized into variable xDoc:



        
相关标签:
1条回答
  • 2021-01-21 17:58

    I'm not sure why this is happening, but removing the xmlns attribute from the body element seems to work:

    var report = XDocument.Parse(
    @"<Report xmlns=""http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition"">
      <ReportSection>
        <Width />
        <Page />
      </ReportSection>
    </Report>");
    
    var body = XElement.Parse(
    @"<Body xmlns=""http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition"">
      <ReportItems />        
      <Height />
      <Style />
    </Body>");
    
    XNamespace ns = report.Root.Name.Namespace;
    if (body.GetDefaultNamespace() == ns)
    {
       body.Attribute("xmlns").Remove();
    }
    
    var node = report.Root.Element(ns + "ReportSection");
    node.AddFirst(body);
    
    0 讨论(0)
提交回复
热议问题