質問

" xmlns:..."を削除するにはどうすればよいですか? C#の各XML要素からの名前空間情報?

役に立ちましたか?

解決

Zombiesheepの注意深い答えにもかかわらず、私の解決策は、これを行うためにxslt変換でxmlを洗浄することです。

wash.xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="no" encoding="UTF-8"/>

  <xsl:template match="/|comment()|processing-instruction()">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="@*">
    <xsl:attribute name="{local-name()}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

</xsl:stylesheet>

他のヒント

ここから http://simoncropp.com/working-around-xml-namespaces

var xDocument = XDocument.Parse(
@"<root>
    <f:table xmlns:f=""http://www.w3schools.com/furniture"">
        <f:name>African Coffee Table</f:name>
        <f:width>80</f:width>
        <f:length>120</f:length>
    </f:table>
  </root>");

xDocument.StripNamespace();
var tables = xDocument.Descendants("table");

public static class XmlExtensions
{
    public static void StripNamespace(this XDocument document)
    {
        if (document.Root == null)
        {
            return;
        }
        foreach (var element in document.Root.DescendantsAndSelf())
        {
            element.Name = element.Name.LocalName;
            element.ReplaceAttributes(GetAttributes(element));
        }
    }

    static IEnumerable GetAttributes(XElement xElement)
    {
        return xElement.Attributes()
            .Where(x => !x.IsNamespaceDeclaration)
            .Select(x => new XAttribute(x.Name.LocalName, x.Value));
    }
}

同様の問題(特定の要素から名前空間属性を削除し、XMLを XmlDocument としてBizTalkに返す必要がある)がありましたが、奇妙な解決策でした。

XML文字列を XmlDocument オブジェクトに読み込む前に、問題のある名前空間属性を削除するためにテキストを置き換えました。 「XML Visualizer」で解析できないXMLになったため、最初は間違っているように見えました。 Visual Studioで。これが最初にこのアプローチをやめた理由です。

ただし、テキストは引き続き XmlDocument にロードでき、BizTalkに出力できました。

以前にも、 childNode.Attributes.RemoveAll()を使用して名前空間属性を削除しようとすると、盲目的な路地に行き当たりました-再び戻ってきました!

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