문제

기본 네임 스페이스와 이름이 지정된 네임 스페이스가 포함 된 XML 문서를 만드는 데 어려움이 있습니다.

<Root xmlns="http://www.adventure-works.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:SchemaLocation="http://www.SomeLocatation.Com/MySchemaDoc.xsd">
  <Book title="Enders Game" author="Orson Scott Card" />
  <Book title="I Robot" author="Isaac Asimov" />
</Root>

하지만 내가 끝내는 것은 이것입니다 ...

<Root xmlns="http://www.adventure-works.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:SchemaLocation="http://www.SomeLocatation.Com/MySchemaDoc.xsd">
  <Book p3:title="Enders Game" p3:author="Orson Scott Card" xmlns:p3="http://www.adventure-works.com" />
  <Book p3:title="I Robot" p3:author="Isaac Asimov" xmlns:p3="http://www.adventure-works.com" />
</Root>

이 XML 스 니펫을 제작하기 위해 쓴 코드는 이것입니다 ...

  XNamespace aw = "http://www.adventure-works.com";
  XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
  XElement root = new XElement(aw + "Root",
      new XAttribute("xmlns", "http://www.adventure-works.com"),
      new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
      new XAttribute(xsi + "SchemaLocation", "http://www.SomeLocatation.Com/MySchemaDoc.xsd"),

      new XElement(aw + "Book",
        new XAttribute(aw + "title", "Enders Game"),
        new XAttribute(aw + "author", "Orson Scott Card")),
      new XElement(aw + "Book",
        new XAttribute(aw + "title", "I Robot"),
        new XAttribute(aw + "author", "Isaac Asimov")));

an을 기반으로합니다 MSDN의 예

****편집하다****

좋아, 더 많은 실험으로 나는 이제 XML 네임 스페이스의 작동 방식에 대해 매우 혼란스러워한다 ....

내가 aw + theattribute를 제거하면 나는 내가 뒤에있는 것을 얻는다. 나는 네임 스페이스가 부모로부터 상속되었다고 생각했는데, 이것은 속성에도 해당되지 않습니까? 속성을 읽는이 코드는 예상대로 작동하지 않기 때문입니다 ...

  XElement xe = XElement.Parse(textBox1.Text);
  XNamespace aw = "http://www.adventure-works.com";
  var qry = from x in xe.Descendants(aw + "Book")
            select (string)x.Attribute(aw + "author");

그러나 속성에서 aw +를 제거하면 기본 네임 스페이스에 속성이 없다고 가정합니다. 이 올바른지?

도움이 되었습니까?

해결책

좋은 질문. 나는 조금 파고 발견했다 이 비트의 XML 사양:

기본 네임 스페이스 선언은 그 범위 내의 모든 반복되지 않은 요소 이름에 적용됩니다. 기본 네임 스페이스 선언은 속성 이름에 직접 적용되지 않습니다. 회복되지 않은 속성의 해석은 나타나는 요소에 의해 결정됩니다.

나중에이 예를 제시합니다.

예를 들어, 각각의 잘못된 빈 요소 태그는 다음에서 불법입니다.

<!-- http://www.w3.org is bound to n1 and n2 -->
<x xmlns:n1="http://www.w3.org" 
   xmlns:n2="http://www.w3.org" >
  <bad a="1"     a="2" />
  <bad n1:a="1"  n2:a="2" />
</x>

그러나 기본 네임 스페이스가 속성 이름에 적용되지 않기 때문에 다음 각각은 합법적이며 두 번째는 다음과 같습니다.

<!-- http://www.w3.org is bound to n1 and is the default -->
<x xmlns:n1="http://www.w3.org" 
   xmlns="http://www.w3.org" >
  <good a="1"     b="2" />
  <good a="1"     n1:a="2" />
</x>

따라서 기본적으로 속성 이름은 기본적으로 네임 스페이스를 얻지 못하는 것처럼 보이며, 이는 당신이 본 모든 것을 설명합니다 :)

다른 팁

XElement doc = XElement.Parse(ToXml());
doc.DescendantsAndSelf().Attributes().Where(a => a.IsNamespaceDeclaration).Remove();
var ele = doc.DescendantsAndSelf();
foreach (var el in ele)
    el.Name = ns != null ? ns + el.Name.LocalName : el.Name.LocalName;

답을 찾기 위해 2 일을 보낸 다른 사람에게.

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