1

読みやすいように 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 要素を追加する方法です。

4

3 に答える 3

1

あなたが抱えている問題は、C# データ構造と目的の XML データ構造との間のスキーマの不一致であるように思えます。

つまり、リストは「フラットな」構造ですが、目的の XML にはネストがあります。

理想的には、2 つがより類似するようにコードをリファクタリングする必要があります。Display_Settings の ac# モデル オブジェクトを作成し、必要に応じてそのクラスに子プロパティを追加できます。これにより、現在のように手動でクランクする代わりに、XML シリアル化に組み込まれた .Net を使用できるようになる可能性があります。

または、手動で XML を作成することに行き詰まっている場合は、XmlElement クラスにあるツリー構造の固有のサポートを使用できます。つまり、ChildNodes プロパティを使用できます: http://msdn.microsoft.com/en-us/library/ system.xml.xmlnode.childnodes.aspx

于 2013-05-31T09:22:36.470 に答える
1

次のようにして、XML の任意の場所に目的のタグを挿入できます。

//Get Display_Settings node
XmlNode displaySettingsNode = xmlDoc.SelectSingleNode(@"Settings/Display_Settings");
//Get LocalPosition node
XmlNode screenNameNode = xmlDoc.SelectSingleNode(@"Settings/Display_Settings/LocalPosition");
//Create Screen Tag
XmlElement screenTag = xmlDoc.CreateElement("ScreenTag");
screenTag.SetAttribute("Tag", (string)nodeDictionary["Tag"].value);
screenTag.InnerText = (string)nodeDictionary["Tag"].value;
//Insert Screen Tag node in desired location
displaySettingsNode.InsertBefore(screenTag, screenNameNode);
于 2013-05-31T09:31:46.433 に答える
0

これはどう?(LinqToXml を使用)

var xDoc = XDocument.Parse(xmlstring); //or XDocument.Load(filename)

var elems = xDoc.Descendants("Display_Settings")
                .First()
                .Elements()
                .Where(e => e.Name.LocalName != "ScreenName")
                .ToList();

elems.ForEach(e => e.Remove());

xDoc.Descendants("ScreenName").First().Add(elems);

var newxml = xDoc.ToString();
于 2013-05-31T09:16:49.197 に答える