BLOG ARTICLE XML | 1 ARTICLE FOUND

  1. 2008.08.14 코딩을 통한 다중 네임스페이스를 지닌 XML 개체 작성법 3

종종보면 다중네임스페이스를 지닌 xml개체를 보게되는데
가지고 있는 책에는 예제가 없어 구글링 하다 알게된 자료..

두가지방법중 난 후자를 선택해서 코딩했다..

Samples are bellow.

As you can see populating XmlDocument with XmlWriter.Write*() methods is much simpler.

string myNS = "http://www.xyz.space.com/land";

// Populating XmlDocument using DOM API:
XmlDocument xdoc1 = new XmlDocument();
XmlElement rootABC = xdoc1.CreateElement("rootABC", myNS);
xdoc1.AppendChild(rootABC);
XmlElement ele1 = xdoc1.CreateElement("ele1", myNS);
rootABC.AppendChild(ele1);
XmlAttribute xsd = xdoc1.CreateAttribute("xmlns", "xsd", "http://www.w3.org/2000/xmlns/");
xsd.Value = "http://www.xyz.space.com/Airschemas";
ele1.Attributes.Append(xsd);
XmlAttribute xsi = xdoc1.CreateAttribute("xmlns", "xsi", "http://www.w3.org/2000/xmlns/");
xsi.Value = "http://www.xyz.space.com/AirschemaInst";
ele1.Attributes.Append(xsi);
XmlElement ObjVal = xdoc1.CreateElement("ObjVal", myNS);
ele1.AppendChild(ObjVal);
XmlElement Name = xdoc1.CreateElement("Name", myNS);
ObjVal.AppendChild(Name);
Name.AppendChild(xdoc1.CreateTextNode("POC2"));

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter w1 = XmlWriter.Create(Console.Out, settings);

xdoc1.WriteTo(w1);
Console.WriteLine();
Console.WriteLine();

// Populating XmlDocument using XPathNavigator API:
XmlDocument xdoc2 = new XmlDocument();
XmlWriter docWriter = xdoc2.CreateNavigator().AppendChild();
docWriter.WriteStartElement("rootABC", myNS);
docWriter.WriteStartElement("ele1", myNS);
docWriter.WriteAttributeString("xmlns", "xsd", "http://www.w3.org/2000/xmlns/", "http://www.xyz.space.com/Airschemas");
docWriter.WriteAttributeString("xmlns", "xsi", "http://www.w3.org/2000/xmlns/", "http://www.xyz.space.com/AirschemaInst");
docWriter.WriteStartElement("ObjVal", myNS);
docWriter.WriteElementString("Name", myNS, "POC2");

docWriter.WriteEndElement();
docWriter.WriteEndElement();
docWriter.WriteEndElement();
docWriter.Close();

XmlWriter w2 = XmlWriter.Create(Console.Out, settings);
xdoc2.WriteTo(w2);
Console.WriteLine();
Console.WriteLine();
w2.Flush();




WebData/Sergey

출처 : http://www.bokebb.com/dev/english/1932/posts/19324822.shtml
AND