Выбор узла через текстовое значение в XML (C#)
Как выбрать узел < text:p text:style-name="P1">TEXT< /text:p> (третья строчка снизу) через текстовое значение TEXT? Цель - нужно скопировать весь этот узел, затем с помощью цикла вставить несколько других значений.
<?xml version="1.0" encoding="UTF-8"?> <office:document-content xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:rpt="http://openoffice.org/2005/report" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:css3t="http://www.w3.org/TR/css3-text/" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0" xmlns:drawooo="http://openoffice.org/2010/draw" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:officeooo="http://openoffice.org/2009/office" office:version="1.2">
<office:scripts />
<office:font-face-decls>
<style:font-face style:name="Mangal1" svg:font-family="Mangal" />
<style:font-face style:name="Liberation Serif" svg:font-family="'Liberation Serif'" style:font-family-generic="roman" style:font-pitch="variable" />
<style:font-face style:name="Liberation Sans" svg:font-family="'Liberation Sans'" style:font-family-generic="swiss" style:font-pitch="variable" />
<style:font-face style:name="Arial" svg:font-family="Arial" style:font-family-generic="system" style:font-pitch="variable" />
<style:font-face style:name="Mangal" svg:font-family="Mangal" style:font-family-generic="system" style:font-pitch="variable" />
<style:font-face style:name="Microsoft YaHei" svg:font-family="'Microsoft YaHei'" style:font-family-generic="system" style:font-pitch="variable" />
<style:font-face style:name="NSimSun" svg:font-family="NSimSun" style:font-family-generic="system" style:font-pitch="variable" />
</office:font-face-decls>
<office:automatic-styles>
<style:style style:name="P1" style:family="paragraph" style:parent-style-name="Standard">
<style:text-properties officeooo:rsid="000044dc" officeooo:paragraph-rsid="000044dc" />
</style:style>
</office:automatic-styles>
<office:body>
<office:text>
<text:sequence-decls>
<text:sequence-decl text:display-outline-level="0" text:name="Illustration" />
<text:sequence-decl text:display-outline-level="0" text:name="Table" />
<text:sequence-decl text:display-outline-level="0" text:name="Text" />
<text:sequence-decl text:display-outline-level="0" text:name="Drawing" />
<text:sequence-decl text:display-outline-level="0" text:name="Figure" />
</text:sequence-decls>
<text:p text:style-name="P1">TEXT</text:p>
</office:text>
</office:body> </office:document-content>
Этот вариант не работает:
XmlDocument xDoc = new XmlDocument();
xDoc.Load("F://content.xml");
XmlElement xRoot = xDoc.DocumentElement;
XmlNode childnode = xRoot.SelectSingleNode("office[text='TEXT']");
if (childnode != null)
Console.WriteLine(childnode.OuterXml);
Ответы (1 шт):
Автор решения: Yitzhak Khabinsky
→ Ссылка
Лучше использовать LINQ to XML при работе с XML.
Нам необходимо учитывать пространства имен (namespaces) XML.
c#
void Main()
{
const string fileName = @"e:\temp\content.xml";
// загрузить документ XML
XDocument xdoc = XDocument.Load(fileName);
// получить оба пространства имен (namespaces)
XNamespace ns1 = xdoc.Root.GetNamespaceOfPrefix("office");
XNamespace ns2 = xdoc.Root.GetNamespaceOfPrefix("text");
// добраться до нужного места
IEnumerable<XElement> xelem = xdoc.Descendants(ns1 + "body")
.Elements(ns1 + "text")
.Elements(ns2 + "p")
.Where(x => x.Value.Equals("TEXT"));
Console.WriteLine(xelem);
}
Результат
<text:p text:style-name="P1" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0">TEXT</text:p>