| item | C# | C++ |
|
1. XML File Open |
using System.Xml;
XmlDocument インスタンス名 = new XmlDocument();
XMLDocument インスタンス名.Load(ファイル名);
|
#using <mscorlib.dll>
#using <System.dll>
#using <System.Data.dll>
#using <System.Xml.dll>
System::Xml::XmlDataDocument^ myXmlDoc=gcnew System::Xml::XmlDataDocument();
myXmlDoc->Load(doc);
myXmlDoc->Save(Console::Out);
|
|
2 Display |
|
myXmlDoc->Save(Console::Out);
|
|
3 Save |
XMLDocument インスタンス名.Save(ファイル名);
|
XmlWriter^ writer;
XmlWriterSettings^ settings;
try{
settings = gcnew XmlWriterSettings();
settings->Indent = true;
settings->IndentChars = ("\t");
settings->OmitXmlDeclaration = true;
writer = XmlWriter::Create("fname.xml", settings); // static call
xml->Save(writer);
} catch (ArgumentNullException^ ex){
Console::WriteLine(ex->ToString());
} catch (ArgumentException^ ex){
Console::WriteLine(ex->ToString());
} catch (XmlException^ ex){
Console::WriteLine(ex->ToString());
} finally {
if(writer != nullptr) writer->Close();
delete settings;
}
|
|
XmlReader・XmlWriter
|
XMLReadterとは、DOM を生成することなく XML 文書を読み込む機能です。また、XMLWriter とは、DOM を生成することなく XML 文書をファイルに出力する機能です。
|
/*
writer->WriteStartElement("book");
writer->WriteElementString("price", "19.95");
writer->WriteEndElement();
writer->Flush();
*/
|
|
4 Create in memory |
private void button1_Click(object
sender, System.EventArgs e)
{
XmlDocument doc = new XmlDocument(); //インスタンスの生成
openFileDialog1.ShowDialog(); //openFileDialogの表示
doc.Load(openFileDialog1.FileName); //XML文書の読み込み
XmlNode root = doc.DocumentElement; //ルートノードの参照
XmlElement elem = doc.CreateElement("要素"); //要素ノードの作成
root.FirstChild.PrependChild(elem); //ルートノードに要素ノードを付加
XmlCharacterData text = doc.CreateTextNode("テキスト"); //テキストノードの作成
//作成した要素ノードにテキストノードを付加
root.FirstChild.FirstChild.PrependChild(text);
saveFileDialog1.ShowDialog(); //SaveFileDialogの表示
doc.Save(saveFileDialog1.FileName); //XMLファイルの保存
}
|
XmlNode^ node;
XmlElement^ root;
XmlElement^ elem;
XmlElement^ elem2;
XmlAttribute^ attr;
XmlAttributeCollection^ attrColl;
String^ xmlns = "http://schemas.microsoft.com/developer/msbuild/2003";
// Create a new element node.
try {
// <Project>
node = xml->CreateNode( XmlNodeType::Element, "Project", xmlns);
xml->AppendChild(node);
root = xml->DocumentElement;
attrColl = xml->DocumentElement->Attributes;
attr = xml->CreateAttribute( "DefaultTargets" );
attr->Value = "Build";
attrColl->Append( attr );
// <PropertyGroup>
node = xml->CreateNode( XmlNodeType::Element,"PropertyGroup", xmlns );
elem = xml->CreateElement("AssemblyName",xmlns);
elem ->InnerText = mc->pname;
node -> AppendChild(elem);
elem = xml->CreateElement("TargetType",xmlns);
elem ->InnerText = mc->targetType;
node -> AppendChild(elem);
root->AppendChild( node );
// Import
node = xml->CreateNode( XmlNodeType::Element, "Import", xmlns );
attrColl = node->Attributes;
attr = xml->CreateAttribute( "Project" );
attr->Value = "$(MSBuildBinPath)\\Microsoft.CSharp.targets";
attrColl->Append( attr );
root->AppendChild( node );
// <Compile>
elem = xml->CreateElement("Compile",xmlns);
attrColl = elem->Attributes;
attr = xml->CreateAttribute( "Include" );
attr->Value = mc->xaml_appdef +".cs";
attrColl->Append( attr );
elem2 = xml->CreateElement("DependentUpon",xmlns);
elem2 ->InnerText = mc->xaml_appdef;
elem -> AppendChild(elem2);
elem2 = xml->CreateElement("SubType",xmlns);
elem2 ->InnerText = "Code";
elem -> AppendChild(elem2);
node -> AppendChild(elem);
} catch (ArgumentException^ ex){
Console::WriteLine(ex->ToString());
} catch (NullReferenceException^ ex){
Console::WriteLine(ex->ToString());
} catch (XmlException^ ex){
Console::WriteLine(ex->ToString());
}
|
|
root node |
XmlNode ルートノード名 = XMLDocumentインスタンス名.DocumentElement;
|
XmlElement^ root = xml->DocumentElement;
|
|
|
|
プロパティ名 |
説明 |
|
DocumentElement |
ルートノードを参照します |
|
ParentNode |
ノードの親ノードを参照します |
|
ChildNodes |
ノードの全ての子ノードを参照します |
|
FirstChild |
ノードの先頭の子ノードを参照します |
|
LastChild |
ノードの最後の子ノードを参照します |
|
Attributes |
ノードの属性を参照します |
|
|
|
get/set |
root.FirstChild.Attributes
|
|
|
Create node/element |
XmlElement 要素ノード名 = XMLDocumentインスタンス名.CreateElement("要素名");
|
|
|
Attribute |
XmlAttribute 属性ノード名 = XMLDocumentインスタンス名.CreateAttribute("属性名");
|
` |
|
Text |
XmlCharacterData テキストノード名 = XMLDocumentインスタンス名.CreateTextNode("テキスト");
|
|
|
|
|
メソッド名 |
説明 |
|
PrependChild |
ノードを子ノードの先頭に追加します |
|
AppendChild |
ノードを子ノードの末尾に追加します |
|
InsertBefore |
ノードを指定したノードの直前に挿入します |
|
InsertAfter |
ノードを指定したノードの直後に挿入します |
|
RemoveChild |
指定した子ノードを削除します |
|
|
|
|
|
メソッド名 |
説明 |
|
Prepend |
属性をノードの属性の先頭に追加します |
|
Append |
属性をノードの属性の末尾に追加します |
|
InsertBefore |
属性を指定した属性の直前に挿入します |
|
InsertAfter |
属性を指定した属性の直後に挿入します |
|
Remove |
指定した属性を削除します |
|
|
|
XPath |
|
|
|
Commnet |
|
<!--
--> |