読みやすいように XML コードを再フォーマットしようとしています。現時点では、次のようになっています。
<Settings>
<Display_Settings>
<ScreenName Name="1">1</ScreenName>
<ScreenTag Tag="1">1</ScreenTag>
<LocalPosition X="1" Y="1" Z="1">0.000, 0.000, 0.000</LocalPosition>
<Width Width="0.000">0.000</Width>
<Height Height="0.000">0.000</Height>
</Display_Settings>
</Settings>
しかし、私はそれが次のように見えるようにしたいです:
<Settings>
<Display_Settings>
<ScreenName Name="1">1
<ScreenTag Tag="1">1</ScreenTag>
<LocalPosition X="1" Y="1" Z="1">0.000, 0.000, 0.000</LocalPosition>
<Width Width="0.000">0.000</Width>
<Height Height="0.000">0.000</Height>
</ScreenName>
</Display_Settings>
</Settings>
わかりました、ゴミの例ですが、要点を理解していただければ幸いです。タグやローカル位置などのすべての値をスクリーン名の子にしたい。これを行うには、通常、次の呼び出しになることがわかりました。
XmlNode _rootNode; // in the above i'll have set this to be Display_Settings
_rootNode.AppendChild(_screenTag);
しかし、あるクラスに保持され、別のクラスに入力されるリストに XML 作成コードを設定しました。次のようになります。
XML を生成する
public HV_WriteXML()
{
//root node
_rootNode = _xmlDoc.CreateElement("InMo_Settings");
_xmlDoc.AppendChild(_rootNode);
_userNode = _xmlDoc.CreateElement("Display_Settings");
_rootNode.AppendChild(_userNode);
}
public void GenereateSettingsFile(List<Node> nodeList, string filePath)
{
_rootNode.RemoveChild(_userNode);
_userNode = _xmlDoc.CreateElement("Display_Settings");
_rootNode.AppendChild(_userNode);
foreach (Node n in nodeList)
{
foreach (XmlElement e in n.GenerateXML(_xmlDoc))
{
_userNode.AppendChild(e);
}
}
_xmlDoc.Save(filePath);
}
次に、これを埋めるために、派生クラスで次のことを行っています。
public override List<XmlElement> GenerateXML(XmlDocument _xmlDoc)
{
List<XmlElement> elementList = new List<XmlElement>();
if (nodeDictionary.ContainsKey("Name "))
{
XmlElement _screenName = _xmlDoc.CreateElement("ScreenName");
_screenName.SetAttribute("Name", (string)nodeDictionary["Name "].value);
_screenName.InnerText = (string)nodeDictionary["Name "].value;
elementList.Add(_screenName);
}
if (nodeDictionary.ContainsKey("Tag"))
{
XmlElement _screenTag = _xmlDoc.CreateElement("ScreenTag");
_screenTag.SetAttribute("Tag", (string)nodeDictionary["Tag"].value);
_screenTag.InnerText = (string)nodeDictionary["Tag"].value;
elementList.Add(_screenTag);
}
}
ここで私の本当の質問は、別のクラスのリストで xml の作成を設定したときに、スクリーン名の子になるように _screenTag 要素を追加する方法です。