문제

C#의 각 XML 요소에서 "XMLNS : ..."네임 스페이스 정보를 제거하려면 어떻게해야합니까?

도움이 되었습니까?

해결책

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로 끝나면 처음에는 잘못된 것 같습니다. 이것이 처음 에이 접근법을 벗어난 것입니다.

그러나 텍스트는 여전히 XmlDocument 그리고 나는 그것을 Biztalk에서 잘 출력 할 수있었습니다.

이전에, 나는 사용하려고 할 때 맹인 골목 하나를 쳤다는 것을 주목하십시오. childNode.Attributes.RemoveAll() 네임 스페이스 속성을 제거하려면 다시 돌아 왔습니다!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top