Using data from InnerText - c#
Is there any option of using data - extracted from InnerText - as the pixels 'y' coordinates position?
I managed to get string elements from .xml file - tag "SequenceInfo".
Example of .xml file:
<SequenceInfo HasSmoke="" Azimuth="267.2" Inclination="682" Zoom="10329" TowerName="Makoszka" Time="2015-03-18 13:10:22">
<Horizon>316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,320,320,320,321,322,322,322,322,322,322,322,322,322,322,322,322,322,322,322,322,322,322,324,324,324,324,324,324,324,326,326,326,326,326,326,326,326,324,324,322,322,322,322,322,322,322,322,322,322,322,322,323,324,326,326,326,326,326,326,326,326,326,326,326,326,326,326,326,326,326,326,324,324,322,322,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320</Horizon>
</SequenceInfo>
I get the text content of the specified node as follows:
316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,320,320,320,321,322,322,322,322,322,322,322,322,322,322,322,322,322,322,322,322,322,322,324,324,324,324,324,324,324,326,326,326,326,326,326,326,326,324,324,322,322,322,322,322,322,322,322,322,322,322,322,323,324,326,326,326,326,326,326,326,326,326,326,326,326,326,326,326,326,326,326,324,324,322,322,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320
Such data are being stored in object 'xtr' (InnerText property).
Can we push this data even further? For example as 'y' coordinates?
Part of my code below:
XmlDocument xtr = new XmlDocument();
string fileName = OFD.FileName;
FileInfo fileInfo = new FileInfo(fileName);
string directoryFullPath = fileInfo.DirectoryName;
fileName = Path.Combine(directoryFullPath, "info.xml");
xtr.Load(fileName);
XmlNodeList list = xtr.GetElementsByTagName("SequenceInfo");
It's unclear if you've actually retrieved the data, and what you want to do with it once you've extracted it...but here's an example that should get you started:
// ... get the data from your XML somehow ...
string data = "316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,316,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,318,320,320,320,321,322,322,322,322,322,322,322,322,322,322,322,322,322,322,322,322,322,322,324,324,324,324,324,324,324,326,326,326,326,326,326,326,326,324,324,322,322,322,322,322,322,322,322,322,322,322,322,323,324,326,326,326,326,326,326,326,326,326,326,326,326,326,326,326,326,326,326,324,324,322,322,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320,320";
// Convert the Text Data to Points:
var points = from y in data.Split(",".ToCharArray()) select new Point(0, int.Parse(y));
// Do something with the Points:
foreach(Point p in points)
{
Console.WriteLine(p.ToString());
}
Problem solved.
Here's my code:
XmlDocument xtr = new XmlDocument();
string fileName = OFD.FileName;
FileInfo fileInfo = new FileInfo(fileName);
string directoryFullPath = fileInfo.DirectoryName;
fileName = Path.Combine(directoryFullPath, "info.xml");
xtr.Load(fileName);
XmlNodeList list = xtr.GetElementsByTagName("SequenceInfo");
string[] punkty = xtr.InnerText.Split(',');
List<Point> punkty1 = new List<Point>();
for (int i = 0; i < punkty.Length; i++)
{
punkty1.Add(new Point { X = i, Y = int.Parse(punkty[i])});
}
Related
add new lines when generating xml from object
this is the code XmlDocument xml = new XmlDocument(); XmlElement root = xml.CreateElement("customers"); xml.AppendChild(root); foreach (var cust in customerlist) { XmlElement child = xml.CreateElement("customer"); child.SetAttribute("CustomerId", cust.CustomerId.ToString()); child.SetAttribute("CustomerName", cust.CustomerName); child.SetAttribute("PhoneNumber", cust.PhoneNumber); child.SetAttribute("Email", cust.Email); root.AppendChild(child); } string s = xml.OuterXml; I want my string to have next lines added to it instead of a single xml document My string is coming as continuous < x >xxxxx< /x > < x >xxxxx< /x >
You can use the XmlTextWriter class to format the XML as a string like this: StringWriter string_writer = new StringWriter(); XmlTextWriter xml_text_writer = new XmlTextWriter(string_writer); xml_text_writer.Formatting = Formatting.Indented; xml.WriteTo(xml_text_writer); // xml is your XmlDocument string formattedXml = string_writer.ToString();
Read more than one file
I am writing a pdf to word converter which works perfectly fine for me. But I want to be able to convert more than one file. What happens now is that it read the first file and does the convert process. public static void PdfToImage() { try { Application application = null; application = new Application(); var doc = application.Documents.Add(); string path = #"C:\Users\Test\Desktop\pdfToWord\"; foreach (string file in Directory.EnumerateFiles(path, "*.pdf")) { using (var document = PdfiumViewer.PdfDocument.Load(file)) { int pagecount = document.PageCount; for (int index = 0; index < pagecount; index++) { var image = document.Render(index, 200, 200, true); image.Save(#"C:\Users\chnikos\Desktop\pdfToWord\output" + index.ToString("000") + ".png", ImageFormat.Png); application.Selection.InlineShapes.AddPicture(#"C:\Users\chnikos\Desktop\pdfToWord\output" + index.ToString("000") + ".png"); } string getFileName = file.Substring(file.LastIndexOf("\\")); string getFileWithoutExtras = Regex.Replace(getFileName, #"\\", ""); string getFileWihtoutExtension = Regex.Replace(getFileWithoutExtras, #".pdf", ""); string fileName = #"C:\Users\Test\Desktop\pdfToWord\" + getFileWihtoutExtension; doc.PageSetup.PaperSize = WdPaperSize.wdPaperA4; foreach (Microsoft.Office.Interop.Word.InlineShape inline in doc.InlineShapes) { if (inline.Height > inline.Width) { inline.ScaleWidth = 250; inline.ScaleHeight = 250; } } doc.PageSetup.TopMargin = 28.29f; doc.PageSetup.LeftMargin = 28.29f; doc.PageSetup.RightMargin = 30.29f; doc.PageSetup.BottomMargin = 28.29f; application.ActiveDocument.SaveAs(fileName, WdSaveFormat.wdFormatDocument); doc.Close(); } } I thought that with my foreach that problem should not occur. And yes there are more than one pdf in this folder
The line var doc = application.Documents.Add(); is outside the foreach loop. So you only create a single word document for all your *.pdf files. Move the above line inside the foreach loop to add a new word document for each *.pdf file.
Reading the string from a txt file and converting it to elements in xml using c#
I have a string like this { {Name Mike} {age 19} {gender male}} in a txt file. I would like this to be converted to xml as the below output. As i am new to this, it seems to be pretty doubts for me. <name>Mike</name> <age>19</age> <gender>male</male> any help would be appreciated.
Here is my solution, at first you have to create a xml file in my case I have created x.xml at my bin folder and must create a root elemnt on the xml file, in my case sample xml at the begening as below, root element name can be anything, I have used just root <root> </root> then code for writting you string as below string s = "{{Name Mike} {age 19} {gender male}}"; string[] s2 = s.Replace("{", "").Replace("}", "").Split(' '); for (int i = 0; i < s2.Length; i++) { XDocument doc = XDocument.Load("x.xml"); XElement rt = doc.Element("root"); XElement elm = rt.Element(s2[i]); if (elm != null) { elm.SetValue(s2[i + 1]); } else { XElement x = new XElement(s2[i], s2[i + 1]); rt.Add(x); } doc.Save("x.xml"); i++; } hope this will solve your problem Update if you want to automate file creation without creating the xml file by hand then you can do this way string s = "{{Name Mike} {age 19} {gender male}}"; string[] s2 = s.Replace("{", "").Replace("}", "").Split(' '); if (!File.Exists("x.xml")) { TextWriter tw = new StreamWriter("x.xml", true); tw.WriteLine("<root>\n</root>"); tw.Close(); } for (int i = 0; i < s2.Length; i++) { XDocument doc = XDocument.Load("x.xml"); XElement rt = doc.Element("root"); XElement elm = rt.Element(s2[i]); if (elm != null) { elm.SetValue(s2[i + 1]); } else { XElement x = new XElement(s2[i], s2[i + 1]); rt.Add(x); } doc.Save("x.xml"); i++; }
Inserting data at specific position in XML
I want to read an XML file and match tag </contrib-group> and write a string after this tag string Final = File.ReadAllText(Npath); string Oxml = path + "\\" + Oword + ".abs.xml"; if (File.Exists(Oxml)) { StreamReader xml = new StreamReader(Oxml,Encoding.UTF8); string xmltag = xml.ReadToEnd(); //File.OpenWrite(Oxml); xml.Close(); StreamWriter write = new StreamWriter(Oxml, true, Encoding.UTF8); Match tag = Regex.Match(xmltag, #"</contrib-group>"); if (tag.Success == true) { write.WriteLine(Environment.NewLine); write.Write(Final); } } So I need to write the string Final to the XML file called Oxml after the matched XML tag </contrib-group>
If you are willing to save the new content as a valid XML file which you can work with, you should use the XML classes for that approach, this should look like this (untested): XmlDocument doc = new XmlDocument(); doc.Load("YourFile.xml"); XmlElement root = doc.DocumentElement; XmlNodeList elemList = root.GetElementsByTagName("contrib-group"); for (int i=0; i < elemList.Count; i++) { XmlNode xnode = elemList[i]; XmlNode xnodeParent = xnode.ParentNode; XMLNode newNode = doc.CreateNode(XmlNodeType.Element, "NodeName", ""); newNode.InnerText = "ContentInsideTheNode"; xnodeParent.InsertAfter(newNode, xnode); } doc.Save("YourFile.xml"); If you only need to replace the string for other purposes than saving it where having a valid XML is not an issue you can just handle it as a string and use the String.Replace (String, String) Method string searchedTag = #"</contrib-group>"; string tagAndNewContent = #"</contrib-group>" + newContent; string fileContentString = File.ReadAllText("YourFile.xml"); string ouput = fileContentString.Replace(searchedTag, tagAndNewContent);
Building xml from scratch and getting result in string?
I'm trying to build up xml document from scratch with use linq-to-xml. XElement root = new XElement("RootNode"); XDocument doc = new XDocument( new XDeclaration("1.0", "utf-8", ""), root ); for (int j = 0; j < 10; j++) { XElement element = new XElement("SetGrid"); element.SetElementValue("ID", j); root.Add(element); } var reader = doc.CreateReader();//doc has 10 elements inside root element string result = reader.ReadInnerXml();//always empty string How can I get string from XDocument?
Just use string result = doc.ToString() or var wr = new StringWriter(); doc.Save(wr); string result = wr.ToString();
One option for empty string as per documentation. XmlReader return: All the XML content, including markup, in the current node. If the current node has no children, an empty string is returned. If the current node is neither an element nor attribute, an empty string is returned. try: XmlReader reader = doc.CreateReader(); reader.Read(); string result = reader.ReadInnerXml()
var wr = new StringWriter(); doc.Save(wr); var xmlString = wr.GetStringBuilder().ToString());
There's a full answer is here. Long story short, you're missing reader.MoveToContent(); i.e. it should be: var reader = root.CreateReader(); reader.MoveToContent(); // <- the missing line string result = reader.ReadInnerXml(); This way the result won't be empty and you even don't have to create XDocument So the full code from the original question + the fix is: XElement root = new XElement("RootNode"); for (int j = 0; j < 10; j++) { XElement element = new XElement("SetGrid"); element.SetElementValue("ID", j); root.Add(element); } var reader = root.CreateReader();// root has 10 elements reader.MoveToContent(); // <-- missing line string result = reader.ReadOuterXml(); // now it returns non-empty string Output: <RootNode><SetGrid><ID>0</ID></SetGrid><SetGrid><ID>1</ID></SetGrid><SetGrid><ID>2</ID></SetGrid><SetGrid><ID>3</ID></SetGrid><SetGrid><ID>4</ID></SetGrid><SetGrid><ID>5</ID></SetGrid><SetGrid><ID>6</ID></SetGrid><SetGrid><ID>7</ID></SetGrid><SetGrid><ID>8</ID></SetGrid><SetGrid><ID>9</ID></SetGrid></RootNode> Note: The code is tested in Visual Studio 2013 / .NET Framework 4.5 MDSN Reference: XmlReader.ReadOuterXml