ぬの部屋(仮)
nu-no-he-ya
  •     123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
       1234
    567891011
    12131415161718
    19202122232425
    26272829   
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
       1234
    567891011
    12131415161718
    19202122232425
    2627282930  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728     
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
     123456
    78910111213
    14151617181920
    21222324252627
    282930    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
       1234
    567891011
    12131415161718
    19202122232425
    2627282930  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
     123456
    78910111213
    14151617181920
    21222324252627
    28      
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
     123456
    78910111213
    14151617181920
    21222324252627
    282930    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
       1234
    567891011
    12131415161718
    19202122232425
    2627282930  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
    1234567
    891011121314
    15161718192021
    22232425262728
           
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    30      
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
     123456
    78910111213
    14151617181920
    21222324252627
    282930    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
         12
    3456789
    10111213141516
    17181920212223
    242526272829 
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    30      
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
        123
    45678910
    11121314151617
    18192021222324
    25262728   
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
       1234
    567891011
    12131415161718
    19202122232425
    2627282930  
           
    1234567
    15161718192021
    293031    
           
         12
    3456789
    10111213141516
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    30      
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
       1234
    567891011
    12131415161718
    19202122232425
    2627282930  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728     
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
     123456
    78910111213
    14151617181920
    21222324252627
    282930    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
     123456
    78910111213
    14151617181920
    21222324252627
    282930    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
          1
    2345678
    9101112131415
    16171819202122
    232425262728 
           
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
  • C++CLIでXDocumentでXML

    準備

    参照の追加

    System::XML::Linqをリンクする

    1. プロジェクトを右クリック
    2. 追加→参照
    3. アセンブリ→System.Xml.Linqをチェック
    4. OKボタン

    名前空間

    以下を先頭に追加

    using namespace System::Xml::Linq;

    使い方

    XML書き出し

    書き出しコード

    using namespace System::Xml::Linq;
    
    int main(array<System::String^>^ args)
    {
    
      // ② 追加する要素を作成
      XElement ^man1;
      {
        XElement^ ext;
    
        ext = gcnew XElement("その他");
        ext->Add(gcnew XElement("特技", L"昼寝"));
    
        man1 =
          gcnew XElement("個人データ",
            gcnew XElement("名前", "山田太郎"),
            gcnew XElement("年齢", 45),
            gcnew XElement("住所", "愛媛県松山市"),
            ext
          );
      }
    
      // ② 追加する要素を作成
      XElement^ man2;
      {
        XElement^ ext;
        ext = gcnew XElement("その他");
        ext->Add(gcnew XElement("特技", L"懸垂"));
    
        man2 =
          gcnew XElement("個人データ",
            gcnew XElement("名前", "西田花子"),
            gcnew XElement("年齢", 15),
            gcnew XElement("住所", "北海道札幌市"),
            ext
          );
      }
    
      // ② 追加する要素を作成
      XElement^ man3;
      {
        XElement^ ext;
        ext = gcnew XElement("その他");
        ext->Add(gcnew XElement("特技", L"読書"));
    
        man3 =
          gcnew XElement("個人データ",
            gcnew XElement("名前", "岡田広美"),
            gcnew XElement("年齢", 35),
            gcnew XElement("住所", "沖縄県中城村"),
            ext
          );
      }
    
      XElement^ root = gcnew XElement("root");
    
      // ① XDocumentオブジェクトを作成
      XDocument^ alldata = gcnew XDocument(
        gcnew XComment("利用者の全情報"),
        root
      );
    
      // ③ XDocumentにXElementを追加
      root->Add(man1);
      root->Add(man2);
      root->Add(man3);
    
      // ④ ファイル保存
      alldata->Save("C:\\mydata\\others\\個人データ1.xml");
    
    }
    

    書き出し結果

    <?xml version="1.0" encoding="utf-8"?>
    <!--利用者の全情報-->
    <root>
      <個人データ>
        <名前>山田太郎</名前>
        <年齢>45</年齢>
        <住所>愛媛県松山市</住所>
        <その他>
          <特技>昼寝</特技>
        </その他>
      </個人データ>
      <個人データ>
        <名前>西田花子</名前>
        <年齢>15</年齢>
        <住所>北海道札幌市</住所>
        <その他>
          <特技>懸垂</特技>
        </その他>
      </個人データ>
      <個人データ>
        <名前>岡田広美</名前>
        <年齢>35</年齢>
        <住所>沖縄県中城村</住所>
        <その他>
          <特技>読書</特技>
        </その他>
      </個人データ>
    </root>
    

    XML読み込み

    読み込みコード

    using namespace System::Xml::Linq;
    
    int main(array<System::String ^> ^args)
    {
    
      // XMLファイルを開く
      XDocument^ xml = XDocument::Load("C:\\mydata\\others\\個人データ1.xml");
    
      XElement^ root = xml->Element("root");
    
      //イテレータの取得
      System::Collections::Generic::IEnumerable<XElement^>^ 
        xelements = root->Elements();
    
      for each (XElement ^ k in xelements)
      {
        // k には<個人データ>の内容が入っている
        System::String^ name = k->Element("名前")->Value;
        int age = int::Parse(k->Element("年齢")->Value);
        System::String^ addr = k->Element("住所")->Value;
    
        System::String^ adv = k->Element("その他")->Element("特技")->Value;
    
        //読み取った内容表示
        System::Console::WriteLine(name);
        System::Console::WriteLine(age);
        System::Console::WriteLine(addr);
        System::Console::WriteLine(adv);
        System::Console::WriteLine("--------------");
    
      }
    
      return 0;
    }
    

    読み込みコード実行結果

    この記事を書いた理由

    CSharpの情報はたくさん出てくる。そしてそれを真似ればうまくいく。99.9%うまくいく。しかしC++CLIから使う場合CSharpのコードからは必要な情報が読み取りにくい。具体的には型情報と名前空間あたりがさっぱりわからない。そもそもC++CLIなんて積極的に使う言語じゃないから自分の中に技術が蓄積しない。加えて情報が少ない。公式にすらろくに無い。その上CSharpの情報で大抵事足りる。故に逆に一度詰まると基本的なことがわからなくて彷徨うことになる。というか、彷徨った。