public void NodeFromNavigator() { string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><root><element>1</element><element /><element>2</element></root>"; XPathDocument doc = new XPathDocument(new StringReader(xml)); XPathNavigator nav = doc.CreateNavigator(); XmlNode node = XmlNodeFactory.Create(nav); MemoryStream mem = new MemoryStream(); XmlTextWriter tw = new XmlTextWriter(mem, System.Text.Encoding.UTF8); tw.Formatting = Formatting.None; ser.Serialize(tw, node); mem.Position = 0; string res = new StreamReader(mem).ReadToEnd(); Assert.AreEqual(xml, res); nav.MoveToRoot(); nav.MoveToFirstChild(); nav.MoveToFirstChild(); node = XmlNodeFactory.Create(nav); mem = new MemoryStream(); tw = new XmlTextWriter(mem, System.Text.Encoding.UTF8); tw.Formatting = Formatting.None; ser.Serialize(tw, node); mem.Position = 0; res = new StreamReader(mem).ReadToEnd(); Assert.AreEqual("<?xml version=\"1.0\" encoding=\"utf-8\"?><element>1</element>", res); }
public void NodeFromReader() { string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><root><element>1</element><element></element><element>2</element></root>"; XmlTextReader tr = new XmlTextReader(new StringReader(xml)); XmlNode node = XmlNodeFactory.Create(tr); MemoryStream mem = new MemoryStream(); XmlTextWriter tw = new XmlTextWriter(mem, System.Text.Encoding.UTF8); tw.Formatting = Formatting.None; ser.Serialize(tw, node); mem.Position = 0; string res = new StreamReader(mem).ReadToEnd(); Assert.AreEqual(xml, res); }
public void NodeFromObject() { Customer cust = new Customer(); cust.FirstName = "Daniel"; cust.LastName = "Cazzulino"; XmlNode node = XmlNodeFactory.Create(cust); MemoryStream mem = new MemoryStream(); XmlTextWriter tw = new XmlTextWriter(mem, System.Text.Encoding.UTF8); tw.Formatting = Formatting.None; ser.Serialize(tw, node); mem.Position = 0; XmlSerializer customerSerializer = new XmlSerializer(typeof(Customer)); Customer result = (Customer)customerSerializer.Deserialize(mem); Assert.AreEqual(cust.FirstName, result.FirstName); Assert.AreEqual(cust.LastName, result.LastName); Assert.AreEqual(cust.BirthDate, result.BirthDate); }