저는 Linq를 XML에 사용하여 새 XML 파일을 만듭니다. 파일의 일부분은 기존 XML 파일에서 가져옵니다. 나는 이것을 위해 다음 코드를 사용한다.XElement가 xmlns를 추가합니다.
var v2 = new XDocument(
new XDeclaration("1.0", "utf-16", ""),
new XComment(string.Format("Converted from version 1. Date: {0}", DateTime.Now)),
new XElement(ns + "keyem",
new XAttribute(XNamespace.Xmlns + "xsd", xsd.NamespaceName),
new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
new XAttribute(xsi + "schemaLocation", schemaLocation.NamespaceName),
new XAttribute("version", "2"),
new XAttribute("description", description),
new XElement(ns + "layout",
new XAttribute("type", type),
new XAttribute("height", height),
new XAttribute("width", width),
settings.Root) // XML from an existing file
문제는 기존 파일의 첫 번째 요소 인 xmlns = ""을 추가하는 것입니다.
결과는 다음과 같습니다
<?xml version="1.0" encoding="utf-16"?>
<foo
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tempuri.org/KeyEmFileSchema.xsd KeyEmFileSchema.xsd"
xmlns="http://tempuri.org/KeyEmFileSchema.xsd">
<settings xmlns="">
...
</settings>
</foo>
나는 이런 모습에서 읽고 있어요 XML 파일을하지만,
<?xml version="1.0" encoding="utf-16"?>
<settings>
<colormaps>
<colormap color="Gray" textcolor="Black"/>
<colormap color="DarkGray" textcolor="White"/>
<colormap color="Black" textcolor="White"/>
<colormap color="Cyan" textcolor="Black"/>
</colormaps>
<macromaps>
<macromap pattern="^@([0-9A-F]{2})\|([0-9A-F]{2})$" replace="{ESC}$1{ESC}$2{MOUSERESET}"/>
<macromap pattern="^\$([0-9A-F]{2})\|([0-9A-F]{2})$" replace="{USERCLICK}{ESC}$1{ESC}$2{MOUSERESET}"/>
<macromap pattern="^\$([0-9A-F]{2})$" replace="{USERCLICK}{ESC}$1"/>
</macromaps>
<keydefault color="Cyan"/>
<groupdefault color="DarkGray"/>
</settings>
나는 그것을 이해하지만 어떻게해야합니까? 나는 defaultSettings.Name = ns + defaultSettings.Name.LocalName을 시도했다. 하지만 모든 하위 요소에 대해이를 수행해야합니다. 더 좋은 것이 틀림 없어. – magol
XSLT 기술을 사용하여 문서를 변환하거나 각 요소를 읽고 코드로 변환해야합니다. 기본적으로로드 한 XDocument는 해당 문서의 각 요소의 네임 스페이스를 알고 있으며 foo와 동일한 네임 스페이스가 아닌 것을 알고 있습니다. –
내가 읽은 XML 파일을 올바른 네임 스페이스로 바꾸려면 어떻게해야합니까? – magol