Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Problem sa xml namespace-om

[es] :: .NET :: Problem sa xml namespace-om

[ Pregleda: 242 | Odgovora: 2 ]

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

DarkMan
Darko Matesic

Član broj: 20445
Poruke: 468
79.101.194.*



Profil

icon Problem sa xml namespace-om14.02.2008. u 00:12

Sledeci kod:
Code:

            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<Project DefaultTargets='Build' xmlns='http://schemas.microsoft.com/developer/msbuild/2003\'></Project>".Replace("'", "\x22"));
            XmlNode node = doc.CreateElement("Proba");
            
            doc.DocumentElement.AppendChild(node);
            Console.WriteLine("Attributes: " + node.Attributes.Count);
            Console.WriteLine("node.OuterXml: " + node.OuterXml);
            Console.WriteLine(doc.OuterXml);


daje sledeci rezultat:
Code:

Attributes: 0
node.OuterXml: <Proba />
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Proba xmlns="" /></Project>


Moj problem je sto mi je u node Proba dodao atribut xmlns="".
node.Attributes.Count kaze da nema atributa a i node.OuterXml ne daje taj xmlns pa pretpostavlja da je nesto do generisanja dokumenta ali ne znam gde.
Znam da se ovo moze ukoniti kada bi uklonio i xmlns iz root-a ali mi on treba.

14.02.2008. u 00:12 

mmix
Miljan Mitrovic
Software Architect
Pancevo, Srbija

Moderator
Član broj: 17944
Poruke: 1612
195.252.78.*



Profil

icon Re: Problem sa xml namespace-om14.02.2008. u 11:03
Moras da navedes default namespace URI za svaki tag koji kreiras a koji ce biti dete tvog root-a. CreateElement ima overload koji prima xmlns namespace elementa i mozes da mu das onaj koji je u root-u:

Code:

            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<Project DefaultTargets=\"Build\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\"></Project>");
            XmlNode node = doc.CreateElement("Proba", doc.DocumentElement.NamespaceURI);

            doc.DocumentElement.AppendChild(node);
            Console.WriteLine("Attributes: " + node.Attributes.Count);
            Console.WriteLine("node.OuterXml: " + node.OuterXml);
            Console.WriteLine(doc.OuterXml);


i sad dobijes:

Code:

Attributes: 0
node.OuterXml: <Proba xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><Proba /></Project>


Attributes je nula zato sto se xmlns= i xmlns:xxx= ne tretira kao atribut od strane XML DOM-a. Ako budes kreirao atribute u tvojim nodovima, prica je drugacija, za razliku od tagova atribut nasledjuje namespace elementa u koji je umetnut. Cak ako navedes isti namespace u CreateAttribute, DOM ce napraviti neku ludoriju ovog tipa (sto je po meni propust u DOMu):

Code:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <Proba d2p1:TestAttr="" xmlns:d2p1="http://schemas.microsoft.com/developer/msbuild/2003" />
</Project>

▪ The word 'politics' is derived from the word 'poly', meaning 'many', and the word 'ticks', meaning 'blood sucking parasites' - Larry Hardiman
▪ If the good guy gets the girl, it's rated PG; if the bad guy gets the girl, it's rated R; and if everybody gets the girl, it's rated X
▪ Illegal aliens have always been a problem in the United States. Ask any Native American
14.02.2008. u 11:03 

DarkMan
Darko Matesic

Član broj: 20445
Poruke: 468
77.46.211.*



Profil

icon Re: Problem sa xml namespace-om14.02.2008. u 13:29
To je to, hvala!
14.02.2008. u 13:29 

[es] :: .NET :: Problem sa xml namespace-om

[ Pregleda: 242 | Odgovora: 2 ]

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.