且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获取XML标记的内部文本并将内部文本保存在文本文件中

更新时间:2023-02-19 16:54:58

这是一种方法



 XNamespace xns =   http://www.opengis.net/kml/2.2; 
XDocument xdInput = XDocument.Load( @ C:\Temp \ input.xml跨度>);
XElement xeDocument = xdInput.Root.Element(xns + Document);
XElement xePlaceMark = xeDocument.Element(xns + Placemark);

XElement xeCoord = xePlaceMark.Descendants(xns + coordinates)。第一();

string [] lines = xeCoord.Value.Trim()。Split(' ');
File.WriteAllLines( @ C:\Temp\Output.txt,线路);





输出:

80.4302618226675,14.10146821639644,0
80.42576847769909,14.01560482206596,0
80.42217788625582,13.94694674204649,0
80.41768407455187,13.86119299421783,0
80.39658103437603,13.79361789706296,0
80.39388165361999,13.7422419928809,0
80.390282415801, 13.67377041492268,0
80.38305143626342,13.53699640691199,0
80.37760577599572,13.43454151969623,0
80.37397453710747,13.36627494143451,0
80.36943988618303,13.28097184062539,0
80.34836784687947,13.21373716074971,0
80.32456264262412,13.09546343074962,0
80.31820720889201,12.97627719905313,0
80.31364934840515,12.89121440605016,0
80.30817653247001,12.78918641302588,0
80.30360336227979,12.70421375659957,0
80.29902651922799,12.61927452126594 ,0
80.29443669587334,12.534377498276 71,0
80.28892262385639,12.43254149390298,0
80.28616076426671,12.38164079913633,0
80.26511197822781,12.31474736051312,0
80.26050279541035,12.22995820537765,0
80.23941074889372,12.16313653744558,0
80.21645688792,12.06243774013701,0
80.17619514615069,11.96267868889682,0
80.15607602451276,11.91279853066779,0
80.11770356329768,11.84691871805963,0
80.07750416467992,11.74715799351077,0
80.02187143303681,11.6821903960103 ,0
79.96351572639186,11.56640475126538,0
79.94065777203802,11.4657303741187,0
79.93510632394116,11.36414102440262,0
79.92860153755805,11.24562697520951,0
79.92393943340687,11.16097451362113,0
79.91740170713379,11.04245799454426,0
79.91646313955498,11.02552624900637,0


OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
    string strFilename = openFileDialog.FileName;
    XmlDocument xmlDoc = new XmlDocument();
    if (File.Exists(strFilename))
    {
        xmlDoc.Load(strFilename);
        string xmlFile = File.ReadAllText(strFilename);
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.LoadXml(xmlFile);
        XmlNodeList nodeList = xmldoc.GetElementsByTagName("coordinates");
        string coordinates = string.Empty;
        foreach (XmlNode node in nodeList)
        {   
            coordinates = node.InnerText;
            string[] lst = coordinates.Split(" ".ToCharArray());
            foreach (string str in lst)
            {
                coordinates += str +  Environment.NewLine;
            }
        }

        SaveFileDialog savefile = new SaveFileDialog();
        savefile.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
        if (savefile.ShowDialog() == DialogResult.OK)
        {
            using (StreamWriter sw = new StreamWriter(savefile.FileName))
            {
                sw.WriteLine(coordinates);
                sw.Flush();
            }
        }
    }
}



[EDIT] Added XML from Solution 1
Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
	<name>Untitled Path.kmz</name>
	<Style id="s_ylw-pushpin_hl">
		<IconStyle>
			<scale>1.3</scale>
			<Icon>
				<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
			</Icon>
			<hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
		</IconStyle>
	</Style>
	<StyleMap id="m_ylw-pushpin">
		<Pair>
			<key>normal</key>
			<styleUrl>#s_ylw-pushpin</styleUrl>
		</Pair>
		<Pair>
			<key>highlight</key>
			<styleUrl>#s_ylw-pushpin_hl</styleUrl>
		</Pair>
	</StyleMap>
	<Style id="s_ylw-pushpin">
		<IconStyle>
			<scale>1.1</scale>
			<Icon>
				<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
			</Icon>
			<hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
		</IconStyle>
	</Style>
	<Placemark>
		<name>Untitled Path</name>
		<styleUrl>#m_ylw-pushpin</styleUrl>
		<LineString>
			<tessellate>1</tessellate>
			<coordinates>
				80.4302618226675,14.10146821639644,0 80.42576847769909,14.01560482206596,0 80.42217788625582,13.94694674204649,0 80.41768407455187,13.86119299421783,0 80.39658103437603,13.79361789706296,0 80.39388165361999,13.7422419928809,0 80.390282415801,13.67377041492268,0 80.38305143626342,13.53699640691199,0 80.37760577599572,13.43454151969623,0 80.37397453710747,13.36627494143451,0 80.36943988618303,13.28097184062539,0 80.34836784687947,13.21373716074971,0 80.32456264262412,13.09546343074962,0 80.31820720889201,12.97627719905313,0 80.31364934840515,12.89121440605016,0 80.30817653247001,12.78918641302588,0 80.30360336227979,12.70421375659957,0 80.29902651922799,12.61927452126594,0 80.29443669587334,12.53437749827671,0 80.28892262385639,12.43254149390298,0 80.28616076426671,12.38164079913633,0 80.26511197822781,12.31474736051312,0 80.26050279541035,12.22995820537765,0 80.23941074889372,12.16313653744558,0 80.21645688792,12.06243774013701,0 80.17619514615069,11.96267868889682,0 80.15607602451276,11.91279853066779,0 80.11770356329768,11.84691871805963,0 80.07750416467992,11.74715799351077,0 80.02187143303681,11.6821903960103,0 79.96351572639186,11.56640475126538,0 79.94065777203802,11.4657303741187,0 79.93510632394116,11.36414102440262,0 79.92860153755805,11.24562697520951,0 79.92393943340687,11.16097451362113,0 79.91740170713379,11.04245799454426,0 79.91646313955498,11.02552624900637,0 
			</coordinates>
		</LineString>
	</Placemark>
</Document>
</kml>



What I have tried:

the o/p obtained for this code is
80.4302618226675,14.10146821639644,0 80.42576847769909,14.01560482206596,0 80.42217788625582,13.94694674204649,0 80.41768407455187,13.86119299421783,0 80.39658103437603,13.79361789706296,0 80.39388165361999,13.7422419928809,0 80.390282415801,13.67377041492268,0 80.38305143626342,13.53699640691199,0 80.37760577599572,13.43454151969623,0 80.37397453710747,13.36627494143451,0 80.36943988618303,13.28097184062539,0
80.4302618226675,14.10146821639644,0
80.42576847769909,14.01560482206596,0
80.42217788625582,13.94694674204649,0
80.41768407455187,13.86119299421783,0
80.39658103437603,13.79361789706296,0
80.39388165361999,13.7422419928809,0
80.390282415801,13.67377041492268,0
80.38305143626342,13.53699640691199,0
80.37760577599572,13.43454151969623,0
80.37397453710747,13.36627494143451,0
80.36943988618303,13.28097184062539,0

you can see here o/p which i actually need to get is this:

80.4302618226675,14.10146821639644,0
80.42576847769909,14.01560482206596,0
80.42217788625582,13.94694674204649,0
80.41768407455187,13.86119299421783,0
80.39658103437603,13.79361789706296,0
80.39388165361999,13.7422419928809,0
80.390282415801,13.67377041492268,0
80.38305143626342,13.53699640691199,0
80.37760577599572,13.43454151969623,0
80.37397453710747,13.36627494143451,0
80.36943988618303,13.28097184062539,0

but before that i dont need the top most value in horizantal manner,can someone help me to correct the code to obtain me the o/p in this manner please
o/p:
80.4302618226675,14.10146821639644,0
80.42576847769909,14.01560482206596,0
80.42217788625582,13.94694674204649,0
80.41768407455187,13.86119299421783,0
80.39658103437603,13.79361789706296,0

This is one way to do it

XNamespace xns = "http://www.opengis.net/kml/2.2";
XDocument xdInput = XDocument.Load(@"C:\Temp\input.xml");
XElement xeDocument = xdInput.Root.Element(xns + "Document");
XElement xePlaceMark = xeDocument.Element(xns + "Placemark");

XElement xeCoord = xePlaceMark.Descendants(xns + "coordinates").First();

string[] lines = xeCoord.Value.Trim().Split(' ');
File.WriteAllLines(@"C:\Temp\Output.txt", lines);



Output:

80.4302618226675,14.10146821639644,0
80.42576847769909,14.01560482206596,0
80.42217788625582,13.94694674204649,0
80.41768407455187,13.86119299421783,0
80.39658103437603,13.79361789706296,0
80.39388165361999,13.7422419928809,0
80.390282415801,13.67377041492268,0
80.38305143626342,13.53699640691199,0
80.37760577599572,13.43454151969623,0
80.37397453710747,13.36627494143451,0
80.36943988618303,13.28097184062539,0
80.34836784687947,13.21373716074971,0
80.32456264262412,13.09546343074962,0
80.31820720889201,12.97627719905313,0
80.31364934840515,12.89121440605016,0
80.30817653247001,12.78918641302588,0
80.30360336227979,12.70421375659957,0
80.29902651922799,12.61927452126594,0
80.29443669587334,12.53437749827671,0
80.28892262385639,12.43254149390298,0
80.28616076426671,12.38164079913633,0
80.26511197822781,12.31474736051312,0
80.26050279541035,12.22995820537765,0
80.23941074889372,12.16313653744558,0
80.21645688792,12.06243774013701,0
80.17619514615069,11.96267868889682,0
80.15607602451276,11.91279853066779,0
80.11770356329768,11.84691871805963,0
80.07750416467992,11.74715799351077,0
80.02187143303681,11.6821903960103,0
79.96351572639186,11.56640475126538,0
79.94065777203802,11.4657303741187,0
79.93510632394116,11.36414102440262,0
79.92860153755805,11.24562697520951,0
79.92393943340687,11.16097451362113,0
79.91740170713379,11.04245799454426,0
79.91646313955498,11.02552624900637,0