コードを書く際に助けてください: 1. xmlファイルを持っています。ノードのタイプに基づいてxmlnodeListのノードを読み込み、フィルタしたいと思います。 2.ルートノードの直前にノード(Boardと言う)を作成し、Boardノードの下に、xmlElementを作成してBoard0、Board1 ..という名前の属性(完了)を作成する必要があります。 3. Board0では、xmlnodeList(コードを記述する必要があります)をコピーします。xmlnodeListをxmlNodeとxmlelementにコピーするには
と仮定、私のxmlファイルは、以下のようになります。
今Switchboard Version="1.4">
<Project Port="7353" WDRemotePort="11160" WatchDogSourceAddress="256" IOProtocolVersion="V3" />
<Controls CountControls="220">
<IOControl Type="AnalogInput" </IOControl>
<IOControl Type="DigitalInput" </IOControl>
<IOControl Type="DigitalInput" </IOControl>
<IOControl Type="DigitalInput" </IOControl>
</Controls>
</Switchboard>
、私は同じファイルは以下のようになりますしたい:
<Switchboard Version="1.4">
<Project Port="7353" WDRemotePort="11160" WatchDogSourceAddress="256" IOProtocolVersion="V3" />
<Controls CountControls="4">
<IOControl Type="AnalogInput" </IOControl>
<IOControl Type="DigitalInput" </IOControl>
<IOControl Type="DigitalInput" </IOControl>
<IOControl Type="DigitalInput" </IOControl>
</Controls>
<Boards CountBoards="3">
<Board0 Name="Alm_HP_RE" BoardWidth="1800" BoardHeigth="1800" Image="" ImageLayout="None">
<IOControl Type="DigitalInput" </IOControl>
<IOControl Type="DigitalInput" </IOControl>
<IOControl Type="DigitalInput" </IOControl>
</Board0>
<Board1 Name="Alm_HP_RE" BoardWidth="1800" BoardHeigth="1800" Image="" ImageLayout="None">
<IOControl Type="AnalogInput" </IOControl>
</Board1>
<Board2 Name="Alm_HP_RE" BoardWidth="1800" BoardHeigth="1800" Image="" ImageLayout="None">
</Board2>
</Boards>
</Switchboard>
コード私が書かれている:
{
XmlDocument hardwareDoc = new XmlDocument();
hardwareDoc.Load(f.FullName);
XmlNode root = hardwareDoc.DocumentElement;
XmlElement board = hardwareDoc.CreateElement("Boards");
root.InsertBefore(board, root.LastChild);
//hardwareDoc.Save(f.FullName);
XmlElement elem = hardwareDoc.CreateElement("Board0");
root.AppendChild(elem);
XmlAttribute attr = hardwareDoc.CreateAttribute("Name");
attr.Value = "DigitalOutput";
elem.Attributes.Append(attr);
attr = hardwareDoc.CreateAttribute("BoardWidth");
attr.Value = "1800";
elem.Attributes.Append(attr);
attr = hardwareDoc.CreateAttribute("BoardHeigth");
attr.Value = "1800";
elem.Attributes.Append(attr);
attr = hardwareDoc.CreateAttribute("Image");
attr.Value = "";
elem.Attributes.Append(attr);
attr = hardwareDoc.CreateAttribute("ImageLayout");
attr.Value = "None";
elem.Attributes.Append(attr);
root.InsertBefore(elem, root.LastChild);
//COPYING _AnalogInputList into element:
hardwareDoc.Save(f.FullName);
}
private static void CreateAttribute()
{
DirectoryInfo plcDirInfo = new DirectoryInfo(@"C:\Users\100755224\Desktop\TEST");
FileInfo[] hardwareFiles = plcDirInfo.GetFiles("test_123.xml");
foreach (FileInfo f in hardwareFiles)
{
XmlDocument hardwareDoc = new XmlDocument();
hardwareDoc.Load(f.FullName);
XmlNode root = hardwareDoc.DocumentElement;
XmlNodeList iosNodeList = hardwareDoc.GetElementsByTagName("IOControl");
break;
}
foreach (FileInfo f in hardwareFiles)
{
XmlDocument hardwareDoc = new XmlDocument();
hardwareDoc.Load(f.FullName);
XmlNode root = hardwareDoc.DocumentElement;
XmlNodeList iosNodeList = hardwareDoc.GetElementsByTagName("IOControl") ;
foreach (XmlNode x in iosNodeList)
{
if (x.Attributes["Type"].Value.Contains("DigitalInput"))
{
if (_AnalogInputList == null)
{
_AnalogInputList = new List<XmlNode>();
}
_AnalogInputList.Add(x);
}
}
XmlElement board = hardwareDoc.CreateElement("Boards-Parent");
root.InsertAfter(board, root.LastChild);
XmlElement elem = hardwareDoc.CreateElement("Board0-Child");
board.AppendChild(elem);
XmlAttribute attr = hardwareDoc.CreateAttribute("Name");
attr.Value = "DigitalOutput";
elem.Attributes.Append(attr);
attr = hardwareDoc.CreateAttribute("BoardWidth");
attr.Value = "1800";
elem.Attributes.Append(attr);
attr = hardwareDoc.CreateAttribute("BoardHeigth");
attr.Value = "1800";
elem.Attributes.Append(attr);
attr = hardwareDoc.CreateAttribute("Image");
attr.Value = "";
elem.Attributes.Append(attr);
attr = hardwareDoc.CreateAttribute("ImageLayout");
attr.Value = "None";
elem.Attributes.Append(attr);
XmlElement iocontrol = hardwareDoc.CreateElement("IOControl");
elem.AppendChild(iocontrol);
if (_AnalogInputList != null)
{
foreach (XmlNode m in _AnalogInputList)
{
elem.AppendChild(m);
}
}
hardwareDoc.Save(f.FullName);
}
} // end of function
を私の関数は正常に動作し、唯一の問題はそれが元のノードを削除し、ノードを後の要素にコピーすることです。私は両方の場所にノードを保持したい。 私は、ノードをクローンする必要があると検索しました。 複製するノードはどれですか?そしてどうやって ? Board0に濾過いるXmlNodeListをコピーする方法
と何あなたの問題を使用してboard0ののXmlElementに
2.Append選択nodelistと必要なノードを選択するには? – JimHawkins
リストをXmlElementにコピーするロジックを配置できません。 – user3115515
foreach(_AnalogInputListのXmlNodem) { elem.AppendChild(m); }例外の取得:挿入するノードは、別のドキュメントコンテキストからのものです。しかし、彼らは同じ文書のものです。提案してください。 – user3115515