Sto cercando di aggiungere nuovi nodi a un file XML esistente. ho questo file con gli elementi prima prova in esso:C#, XML, aggiunta di nuovi nodi
<?xml version="1.0" encoding="utf-8"?>
<Root xmlns="http://prpa.org/XMLSchema1.xsd">
<studenti>
<student>
<ime>test</ime>
<prezime>test</prezime>
<ocijena>0</ocijena>
</student>
</studenti>
<profesori>
<profesor>
<ime>test</ime>
<prezime>test</prezime>
</profesor>
</profesori>
</Root>
Ho usato questo schema per generare questo documento XML
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema1"
targetNamespace="http://prpa.org/XMLSchema1.xsd"
elementFormDefault="qualified"
xmlns="http://prpa.org/XMLSchema1.xsd"
xmlns:mstns="http://prpa.org/XMLSchema1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:element name='Root'>
<xs:complexType>
<xs:sequence>
<xs:element name="studenti">
<xs:complexType>
<xs:sequence>
<xs:element name="student">
<xs:complexType>
<xs:sequence>
<xs:element name="ime" type="xs:string"/>
<xs:element name="prezime" type="xs:string"/>
<xs:element name="ocijena" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="profesori">
<xs:complexType>
<xs:sequence>
<xs:element name="profesor">
<xs:complexType>
<xs:sequence>
<xs:element name="ime" type="xs:string"/>
<xs:element name="prezime" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Ora, ho bisogno di aggiungere nuovo nodo
<profesor>
<ime>test2</ime>
<prezime>test2</prezime>
</profesor>
Ho provato fino ad ora:
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("data/sve.xml"));
XmlNode root = xmldoc.SelectSingleNode("root/profesori", null);
XmlNode prof = xmldoc.CreateNode(XmlNodeType.Element, "profesor", null);
XmlNode ime = xmldoc.CreateNode(XmlNodeType.Element, "ime", null);
ime.InnerText = name;
prof.AppendChild(ime);
XmlNode prezime = xmldoc.CreateNode(XmlNodeType.Element, "prezime", null);
prezime.InnerText = surname;
prof.AppendChild(prezime);
root.AppendChild(prof);
xmldoc.Save(Server.MapPath("data/sve.xml"));
Ho anche provato ad aggiungere namespace Menager ad esso:
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmldoc.NameTable);
nsMgr.AddNamespace("ns", xmldoc.NamespaceURI);
XmlNode root = xmldoc.SelectSingleNode("/ns:root/ns:profesori", nsMgr);
ed ancora non posso selezionare nodo padre e aggiungere nuovi nodo figlio ad esso. In modalità debug, l'oggetto "root" è nullo con o senza spazio dei nomi, quindi alla fine I off ottiene un'eccezione di puntatore nullo.
Cosa sto sbagliando?
P.S. Schemi, namespace, file xml sono tutti locali e scritti da me, se questo fa alcuna differenza ...
ty per la risposta, ma il nodo "root" rimane nul l. Ho provato con tutto lo spazio dei nomi e ora ho implementato la tua soluzione con "string strNamespace = xmldoc.DocumentElement.NamespaceURI;", ma ancora nullo. – klo
post malato ultimo stato nella nuova risposta – klo
@klo Un altro problema è che stai usando "ns: root" invece di "ns: Root". XML e XPath sono case sensitive. – JLRishe