何が起こっているのですか\違いは何ですか?XMLファイルから特定のノードを返そうとしています。
<?xml version="1.0" encoding="utf-8"?>
<JMF SenderID="InkZone-Controller" Version="1.2">
<Command ID="cmd.00695" Type="Resource">
<ResourceCMDParams ResourceName="InkZoneProfile" JobID="K_41">
<InkZoneProfile ID="r0013" Class="Parameter" Locked="false" Status="Available" PartIDKeys="SignatureName SheetName Side Separation" DescriptiveName="Schieberwerte von DI" ZoneWidth="32">
<InkZoneProfile SignatureName="SIG1">
<InkZoneProfile Locked="False" SheetName="S1">
<InkZoneProfile Side="Front" />
</InkZoneProfile>
</InkZoneProfile>
</InkZoneProfile>
</ResourceCMDParams>
</Command>
<InkZoneProfile Separation="Cyan" ZoneSettingsX="0 0,005 " />
</JMF>
コード:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("C:\\test\\test.xml");
XmlNode root = xmlDoc.DocumentElement;
var parent = root.SelectSingleNode("/JMF/Command/ResourceCmdParams/InkZoneProfile/InkZoneProfile/InkZoneProfile/InkZoneProfile");
XmlElement IZP = xmlDoc.CreateElement("InkZoneProfile");
IZP.SetAttribute("Separation", x.colorname);
IZP.SetAttribute("ZoneSettingsX", x.colorvalues);
xmlDoc.DocumentElement.AppendChild(IZP);
xmlDoc.Save("C:\\test\\test.xml");
varparentは私にnullを返します。デバッグしましたが、rootとxmlDocの内部テキストにXMLコンテンツがあります。しかし、ここで行われたテスト(前の質問でユーザー@ har07によって行われた:SelectSingleNodeは、名前空間を管理していてもnullを返します。問題なく動作しました。https://dotnetfiddle.net/vJ8h9S
これら2つの違いは何ですか?それらは基本的に同じコードに従いますが、一方は機能し、もう一方は機能しません。
デバッグ中に、root.InnerXmlのコンテンツがロードされていることがわかりました(XmlDoc.InnerXmlと同じ)。ただし、InnerXmlはSelectSingleNodeへのメソッドを実装していません。文字列に保存すると、インデントなどが失われる可能性があると思います。
誰かが違いは何ですか、何が間違っているのか教えてもらえますか?ありがとう!XMLサンプル:https://drive.google.com/file/d/0BwU9_GrFRYrTUFhMYWk5blhhZWM/view?usp = shareing
SetAttribute
文字列を自動エスケープしないでください。したがって、XMLファイルが無効になります。
XmlElement.SetAttributeについてのMSDNから
エンティティ参照として認識される構文などのマークアップは、リテラルテキストとして扱われ、書き出されるときに実装によって適切にエスケープされる必要があります。
コード内ですべての行に含まれているものを見つけ、値をエスケープするためにSetAttribute
使用SecurityElement.Escape
します。
例:次の行を変更します。
IZP.SetAttribute("Separation", x.colorname);
IZP.SetAttribute("ZoneSettingsX", x.colorvalues);
に:
using System.Security;
IZP.SetAttribute("Separation", SecurityElement.Escape(x.colorname));
IZP.SetAttribute("ZoneSettingsX", SecurityElement.Escape(x.colorvalues));
属性に名前が含まれている場合は<>"'&
、値のようにエスケープする必要があります。
注意:
古いコードを使用して作成した現在のxmlは無効であるため、ロードすると例外が発生するため、削除する必要があります。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加