定義済みの名前仕様を追加したい xml ファイルがあります。コードは次のとおりです。
private const string uri = "http://www.w3.org/TR/html4/";
private static readonly List<string> namespaces = new List<string> { "lun" };
public static XElement AddNameSpaceAndLoadXml(string xmlFile) {
var nameSpaceManager = new XmlNamespaceManager(new NameTable());
// add custom namespace to the manager and take the prefix from the collection
namespaces.ToList().ForEach(name => {
nameSpaceManager.AddNamespace(name, string.Concat(uri, name));
});
XmlParserContext parserContext = new XmlParserContext(null, nameSpaceManager, null, XmlSpace.Default);
using (var reader = XmlReader.Create(@xmlFile, null, parserContext)) {
return XElement.Load(reader);
}
}
問題は、メモリ内の結果の xml に、追加された正しい名前空間が表示されないことです。また、ルートには追加されず、タグの横に追加されます。以下に Xml を追加します。xmlでは、表示されp3:read_data
ているはずですlun:read_data
。
ルート タグに名前空間を追加し、間違った名前を取得しないようにするにはどうすればよいですか。
サンプル入力 xml:
<config file-suffix="perf">
<overview-graph title="Top 5 LUN Reads" max-series="5" remove-series="1">
<counters lun:read_data=""/>
</overview-graph>
</config>
出力 xml が期待されます:
<config file-suffix="perf" xmlns:lun="http://www.w3.org/TR/html4/lun">
<overview-graph title="Top 5 LUN Reads" max-series="5" remove-series="1">
<counters lun:read_data="" />
</overview-graph>
</config>
上記のコードを使用した出力:
<config file-suffix="perf" >
<overview-graph title="Top 5 LUN Reads" max-series="5" remove-series="1">
<counters p3:read_data="" xmlns:p3="http://www.w3.org/TR/html4/lun"/>
</overview-graph>
</config>