// Check the properties on a newly constructed whitespace node. private void CheckProperties(String msg, XmlSignificantWhitespace white, String value, bool failXml) { String temp; AssertEquals(msg + " [1]", "#significant-whitespace", white.LocalName); AssertEquals(msg + " [2]", "#significant-whitespace", white.Name); AssertEquals(msg + " [3]", String.Empty, white.Prefix); AssertEquals(msg + " [4]", String.Empty, white.NamespaceURI); AssertEquals(msg + " [5]", XmlNodeType.SignificantWhitespace, white.NodeType); AssertEquals(msg + " [6]", value, white.Data); AssertEquals(msg + " [7]", value, white.Value); AssertEquals(msg + " [8]", value, white.InnerText); AssertEquals(msg + " [9]", value.Length, white.Length); AssertEquals(msg + " [10]", String.Empty, white.InnerXml); if(failXml) { try { temp = white.OuterXml; Fail(msg + " [11]"); } catch(ArgumentException) { // Success } } else { AssertEquals(msg + " [12]", value, white.OuterXml); } }
// Check the properties on a newly constructed whitespace node. private void CheckProperties(String msg, XmlSignificantWhitespace white, String value, bool failXml) { String temp; AssertEquals(msg + " [1]", "#significant-whitespace", white.LocalName); AssertEquals(msg + " [2]", "#significant-whitespace", white.Name); AssertEquals(msg + " [3]", String.Empty, white.Prefix); AssertEquals(msg + " [4]", String.Empty, white.NamespaceURI); AssertEquals(msg + " [5]", XmlNodeType.SignificantWhitespace, white.NodeType); AssertEquals(msg + " [6]", value, white.Data); AssertEquals(msg + " [7]", value, white.Value); AssertEquals(msg + " [8]", value, white.InnerText); AssertEquals(msg + " [9]", value.Length, white.Length); AssertEquals(msg + " [10]", String.Empty, white.InnerXml); if (failXml) { try { temp = white.OuterXml; Fail(msg + " [11]"); } catch (ArgumentException) { // Success } } else { AssertEquals(msg + " [12]", value, white.OuterXml); } }
public Sample() { XmlDocument doc = new XmlDocument(); doc.LoadXml("<!-- Sample XML fragment -->" + "<author xml:space='preserve'>" + "<first-name>Eva</first-name>" + "<last-name>Corets</last-name>" + "</author>"); Console.WriteLine("InnerText before..."); Console.WriteLine(doc.DocumentElement.InnerText); // Add white space. currNode = doc.DocumentElement; XmlSignificantWhitespace sigws = doc.CreateSignificantWhitespace("\t"); currNode.InsertAfter(sigws, currNode.FirstChild); Console.WriteLine(); Console.WriteLine("InnerText after..."); Console.WriteLine(doc.DocumentElement.InnerText); // Save and then display the file. doc.Save(filename); Console.WriteLine(); Console.WriteLine("Reading file..."); ReadFile(filename); }
private async Task Dump_Out_XML() { Debug.WriteLine("Debugging Dump..."); XmlDocument outputXML = new XmlDocument(); XmlElement rootElement = outputXML.CreateElement(string.Empty, "stations", string.Empty); outputXML.AppendChild(rootElement); XmlSignificantWhitespace sigws = outputXML.CreateSignificantWhitespace("\n\t"); rootElement.InsertAfter(sigws, rootElement.FirstChild); foreach (Podcast thisOne in PodList.Items) { XmlElement stationElement = outputXML.CreateElement(string.Empty, "station", string.Empty); XmlElement stationNameElement = outputXML.CreateElement(string.Empty, "stationName", string.Empty); XmlText text1 = outputXML.CreateTextNode(thisOne.stationName); stationNameElement.AppendChild(text1); stationElement.AppendChild(stationNameElement); stationElement.InsertAfter(sigws, stationNameElement); XmlElement stationURLElement = outputXML.CreateElement(string.Empty, "stationURL", string.Empty); XmlText text2 = outputXML.CreateTextNode(thisOne.stationURL); stationURLElement.AppendChild(text2); stationElement.AppendChild(stationURLElement); stationElement.InsertAfter(sigws, stationURLElement); rootElement.AppendChild(stationElement); Debug.WriteLine(thisOne.stationName); Debug.WriteLine(thisOne.stationURL); } Debug.WriteLine(outputXML.InnerXml.ToString()); StorageFolder PodBlasterFolder = await KnownFolders.MusicLibrary.GetFolderAsync("Podblaster"); string XMLFilePath = PodBlasterFolder.Path + @"\stations.xml"; StorageFile XMLFile = await PodBlasterFolder.GetFileAsync("stations.xml"); await FileIO.WriteTextAsync(XMLFile, outputXML.InnerXml.ToString()); Debug.WriteLine("Wrote out XML File!"); }
public void DataAndValue() { string val = "\t\t\r\n "; whitespace = doc2.CreateSignificantWhitespace(val); Assert.AreEqual(val, whitespace.Data, "#DataValue.1"); Assert.AreEqual(val, whitespace.Value, "#DataValue.2"); whitespace.Value = val + "\t"; Assert.AreEqual(val + "\t", whitespace.Data, "#DataValue.3"); }
public void XmlSignificantWhitespaceBadConstructor() { try { broken = document.CreateSignificantWhitespace("black"); } catch (ArgumentException) { return; } catch (Exception) { Assert.Fail("Incorrect Exception thrown."); } }
public void GetReady() { document = new XmlDocument(); document.LoadXml("<root><foo></foo></root>"); XmlElement element = document.CreateElement("foo"); whitespace = document.CreateSignificantWhitespace("\r\n"); element.AppendChild(whitespace); doc2 = new XmlDocument(); }
//</Snippet5> //************************************************************************************ // // Add an element to the XML document at a specific location // Takes a string that describes where the user wants the new node // to be positioned. The string comes from a series of radio buttons in a UI. // this method also accepts the XMLDocument in context. You have to use the // this instance because it is the object that was used to generate the // selectedBook XMLNode. // //************************************************************************************ //</Snipppet9> public void InsertBookElement(XmlElement bookElement, string position, XmlNode selectedBook, bool validateNode, bool generateSchema) { XmlDocument doc = bookElement.OwnerDocument; string stringThatContainsNewline = bookElement.OuterXml; switch (position) { case Constants.positionTop: // Add newline characters and spaces to make XML more readable. XmlSignificantWhitespace sigWhiteSpace = doc.CreateSignificantWhitespace("\n "); doc.DocumentElement.InsertBefore(sigWhiteSpace, doc.DocumentElement.FirstChild); doc.DocumentElement.InsertAfter(bookElement, doc.DocumentElement.FirstChild); break; case Constants.positionBottom: // Add newline characters to make XML more readable. XmlWhitespace whitespace = doc.CreateWhitespace(" "); XmlNode appendedNode = doc.DocumentElement.AppendChild(bookElement); doc.DocumentElement.InsertBefore(whitespace, appendedNode); sigWhiteSpace = doc.CreateSignificantWhitespace("\n"); doc.DocumentElement.InsertAfter(sigWhiteSpace, appendedNode); break; case Constants.positionAbove: // Add newline characters to make XML more readable. XmlNode currNode = doc.DocumentElement.InsertBefore(bookElement, selectedBook); sigWhiteSpace = doc.CreateSignificantWhitespace("\n "); doc.DocumentElement.InsertAfter(sigWhiteSpace, currNode); break; case Constants.positionBelow: // Add newline characters to make XML more readable. sigWhiteSpace = doc.CreateSignificantWhitespace("\n "); XmlNode whiteSpaceNode = doc.DocumentElement.InsertAfter(sigWhiteSpace, selectedBook); doc.DocumentElement.InsertAfter(bookElement, whiteSpaceNode); break; default: doc.DocumentElement.AppendChild(bookElement); break; } if (validateNode) { validateXML(generateSchema, doc); } }
private void button1_Click(object sender, EventArgs e) { string path = Application.StartupPath + @"\sample.xml"; XmlDocument doc = new XmlDocument(); XmlElement root = doc.CreateElement("root"); XmlElement fullname = doc.CreateElement("fullname"); XmlElement address = doc.CreateElement("address"); XmlText fullnametext = doc.CreateTextNode(textBox1.Text); XmlText street1 = doc.CreateTextNode(textBox2.Text); XmlText street2 = doc.CreateTextNode(textBox3.Text); XmlText city = doc.CreateTextNode(textBox4.Text); XmlText state = doc.CreateTextNode(textBox5.Text); XmlText country = doc.CreateTextNode(textBox6.Text); XmlSignificantWhitespace sws1 = doc.CreateSignificantWhitespace("\r\n"); XmlSignificantWhitespace sws2 = doc.CreateSignificantWhitespace("\r\n"); XmlSignificantWhitespace sws3 = doc.CreateSignificantWhitespace("\r\n"); XmlSignificantWhitespace sws4 = doc.CreateSignificantWhitespace("\r\n"); XmlSignificantWhitespace sws5 = doc.CreateSignificantWhitespace("\r\n"); fullname.AppendChild(fullnametext); address.AppendChild(street1); address.AppendChild(sws1); address.AppendChild(street2); address.AppendChild(sws2); address.AppendChild(city); address.AppendChild(sws3); address.AppendChild(state); address.AppendChild(sws4); address.AppendChild(country); address.AppendChild(sws5); doc.AppendChild(root); root.AppendChild(fullname); root.AppendChild(address); doc.Save(path); }
public static void Main() { XmlDocument doc = new XmlDocument(); doc.LoadXml("<author xml:space='preserve'>" + "<first-name>Eva</first-name>" + "<last-name>Corets</last-name>" + "</author>"); Console.WriteLine("InnerText before..."); Console.WriteLine(doc.DocumentElement.InnerText); // Add white space. XmlNode currNode = doc.DocumentElement; XmlSignificantWhitespace sigws = doc.CreateSignificantWhitespace("\t"); currNode.InsertAfter(sigws, currNode.FirstChild); Console.WriteLine(); Console.WriteLine("InnerText after..."); Console.WriteLine(doc.DocumentElement.InnerText); }
private void Stream(XmlSignificantWhitespace swSpace) { MDataObjs.Add(new ClassSeparator(typeof(XmlSignificantWhitespace))); // no data to display at this level }
private void MergeIntoDestination() { XmlNodeList nodes; var levenshtein = new NormalizedLevenshtein(); var root = _destDoc.DocumentElement; if (_replace) { nodes = root.SelectNodes("//xliff:trans-unit", _nsmgr); } else { nodes = root.SelectNodes("//xliff:trans-unit[not(xliff:target)]", _nsmgr); } foreach (XmlNode node in nodes) { var id = node.Attributes["id"].Value; if (_translations.ContainsKey(id)) { var source = node.SelectSingleNode("xliff:source", _nsmgr); var transSource = _translations[id].SelectSingleNode($"./xliff:source", _nsmgr); var transTarget = _translations[id].SelectSingleNode($"./xliff:target", _nsmgr); if (source.InnerText != transSource.InnerText) { var percentSimilar = Math.Round((1 - levenshtein.Distance(source.InnerText, transSource.InnerText)) * 100); if (_verbose) { Console.WriteLine($"Sources mismatch in id='{id}' Similarity {percentSimilar}%."); Console.WriteLine($" Source file='{transSource.InnerText}'"); Console.WriteLine($" Target file='{source.InnerText}'"); } if (percentSimilar < _fuzzy) { if (_verbose) { Console.WriteLine($"Skipping"); } continue; } } if (_replace) { var oldTarget = node.SelectSingleNode("xliff:target", _nsmgr); if (oldTarget != null) { node.RemoveChild(oldTarget); } } if (source.NextSibling.Name != "#significant-whitespace") { XmlSignificantWhitespace sigws = _destDoc.CreateSignificantWhitespace("\n "); node.InsertAfter(sigws, source); } XmlNode target = _destDoc.ImportNode(transTarget, true); node.InsertAfter(target, source.NextSibling); if (target.NextSibling.Name != "#significant-whitespace") { XmlSignificantWhitespace sigws = _destDoc.CreateSignificantWhitespace("\n "); node.InsertAfter(sigws, target); } } } }
public override void VisitSignificantWhitespace(XmlSignificantWhitespace significantWhitespace) { this.Append(significantWhitespace.InnerText, XmlDocRichTextPresenterEx.ourNormalStyle); base.VisitSignificantWhitespace(significantWhitespace); }
private void Stream(XmlSignificantWhitespace significantWhitespace) { Data.Add(new ClassSeparatorData(typeof(XmlSignificantWhitespace))); }
void CopyNode(XmlDocument newDoc, XmlNode from, XmlNode toParent) { if (RemoveAll && from.NodeType != XmlNodeType.Element) { return; } XmlNode child = null; bool newLineNode = false; switch (from.NodeType) { case XmlNodeType.Element: newLineNode = true; if (RemoveNamespacesAndPrefixes) { child = newDoc.CreateElement(from.LocalName); } else { XmlElement e = from as XmlElement; child = newDoc.CreateElement(e.Prefix, e.LocalName, e.NamespaceURI); } break; case XmlNodeType.Attribute: { if (RemoveAttributes) { return; } XmlAttribute fromAttr = from as XmlAttribute; if (!fromAttr.Specified) { return; } XmlAttribute a; if (RemoveNamespacesAndPrefixes) { a = newDoc.CreateAttribute(fromAttr.LocalName); } else { a = newDoc.CreateAttribute(fromAttr.Prefix, fromAttr.LocalName, fromAttr.NamespaceURI); } toParent.Attributes.Append(a); CopyNodes(newDoc, from, a); return; } case XmlNodeType.CDATA: newLineNode = true; child = newDoc.CreateCDataSection((from as XmlCDataSection).Data); break; case XmlNodeType.Comment: if (RemoveWhiteSpace) { return; } newLineNode = true; child = newDoc.CreateComment((from as XmlComment).Data); break; case XmlNodeType.ProcessingInstruction: newLineNode = true; XmlProcessingInstruction pi = from as XmlProcessingInstruction; child = newDoc.CreateProcessingInstruction(pi.Target, pi.Data); break; case XmlNodeType.DocumentType: newLineNode = true; toParent.AppendChild(from.CloneNode(true)); return; case XmlNodeType.EntityReference: child = newDoc.CreateEntityReference((from as XmlEntityReference).Name); break; case XmlNodeType.SignificantWhitespace: if (RemoveWhiteSpace) { return; } child = newDoc.CreateSignificantWhitespace(from.Value); break; case XmlNodeType.Text: if (RemoveText) { return; } newLineNode = true; child = newDoc.CreateTextNode(from.Value); break; case XmlNodeType.Whitespace: if (RemoveWhiteSpace) { return; } child = newDoc.CreateWhitespace(from.Value); break; case XmlNodeType.XmlDeclaration: newLineNode = true; XmlDeclaration d = from as XmlDeclaration; XmlDeclaration d1 = newDoc.CreateXmlDeclaration(d.Version, d.Encoding, d.Standalone); newDoc.InsertBefore(d1, newDoc.DocumentElement); return; } if (NewLines && newLineNode && toParent.NodeType != XmlNodeType.Attribute) { XmlSignificantWhitespace s = newDoc.CreateSignificantWhitespace("\r\n"); toParent.AppendChild(s); } toParent.AppendChild(child); CopyNodes(newDoc, from, child); }
public void InnerAndOuterXml() { whitespace = doc2.CreateSignificantWhitespace("\r\n\t "); Assert.AreEqual(String.Empty, whitespace.InnerXml); Assert.AreEqual("\r\n\t ", whitespace.OuterXml); }
private async Task OOBE() { // If you haven't used the application before, this will bootstrap you with a sample XML file // and create the root folder and download subfolder. // Let's look for the folders... try { StorageFolder podBlasterParent = await KnownFolders.MusicLibrary.GetFolderAsync(@"PodBlaster"); StorageFolder podBlasterDownloads = await KnownFolders.MusicLibrary.GetFolderAsync(@"PodBlaster\Downloads"); } catch { StorageFolder musicLibrary = await KnownFolders.MusicLibrary.CreateFolderAsync(@"PodBlaster"); StorageFolder musicLibraryDownloads = await musicLibrary.CreateFolderAsync(@"Downloads"); } // Let's look for an XML file... try { StorageFolder podBlasterParent = await KnownFolders.MusicLibrary.GetFolderAsync(@"PodBlaster"); StorageFile podBlasterXML = await podBlasterParent.GetFileAsync(@"stations.xml"); } catch { StorageFolder podBlasterParent = await KnownFolders.MusicLibrary.GetFolderAsync(@"PodBlaster"); XmlDocument outputXML = new XmlDocument(); XmlElement rootElement = outputXML.CreateElement(string.Empty, "stations", string.Empty); outputXML.AppendChild(rootElement); XmlSignificantWhitespace sigws = outputXML.CreateSignificantWhitespace("\n\t"); rootElement.InsertAfter(sigws, rootElement.FirstChild); List <Podcast> starterList = new List <Podcast>(); Podcast replyAll = new Podcast(); replyAll.stationName = "Reply All"; replyAll.stationURL = "http://feeds.gimletmedia.com/hearreplyall"; starterList.Add(replyAll); foreach (Podcast thisOne in starterList) { XmlElement stationElement = outputXML.CreateElement(string.Empty, "station", string.Empty); XmlElement stationNameElement = outputXML.CreateElement(string.Empty, "stationName", string.Empty); XmlText text1 = outputXML.CreateTextNode(thisOne.stationName); stationNameElement.AppendChild(text1); stationElement.AppendChild(stationNameElement); stationElement.InsertAfter(sigws, stationNameElement); XmlElement stationURLElement = outputXML.CreateElement(string.Empty, "stationURL", string.Empty); XmlText text2 = outputXML.CreateTextNode(thisOne.stationURL); stationURLElement.AppendChild(text2); stationElement.AppendChild(stationURLElement); stationElement.InsertAfter(sigws, stationURLElement); rootElement.AppendChild(stationElement); } Debug.WriteLine(outputXML.InnerXml.ToString()); string XMLFilePath = podBlasterParent.Path + @"\stations.xml"; StorageFile XMLFile = await podBlasterParent.CreateFileAsync("stations.xml"); await FileIO.WriteTextAsync(XMLFile, outputXML.InnerXml.ToString()); Debug.WriteLine("Wrote out XML File!"); } }