views:

73

answers:

2

hi i have an xml like this

<?xml version="1.0"?>
<DataSetExchangeWMS xmlns="http://tempuri.org/DataSetExchangeWMS.xsd"&gt;
  <dtObjektInfo>
    <LanguageCode>1031</LanguageCode>
    <LiegenschaftID>7463</LiegenschaftID>
  </dtObjektInfo>

1040 7463 09

Now as i learned from tutorial i get my xml file and select node from descendant.

now why the first foreache does not work? i need to create complicated queries.

XDocument test = XDocument.Load(Server.MapPath("~/XML/objects.xml"));

var objecte = from i in test.Root.Descendants("dtObjektInfo")
              select i;

// new test with where
        var objecte = from i in test.Descendants( ns + "dtObjektInfo")
                      where i.Element("LanguageCode").Value == "1031"
                      select i;

foreach (var item1 in objecte)
{
    Response.Write(item1.Value);
}

foreach (XElement item in test.Root.Nodes())
{
    Response.Write(item.Value + "<br />");
}
+1  A: 

You're not using the XML namespace defined in your XML document!

You need to specify the XML namespace when you query your XML:

XNamespace ns = "http://tempuri.org/DataSetExchangeWMS.xsd";

var objecte = from i in test.Root.Descendants(ns + "dtObjektInfo")
              select i;

Marc

marc_s
hmm what should I add
snarebold
you need to define the namespace to match the one defined in your XML file, and then use that XNamespace in all your queries
marc_s
thank you but still no result. code looks same like yours.
snarebold
"Works on my machine" :-) I was able to select and get back a single record - no problem.
marc_s
is the file really being loaded into the XDocument? Where did you put the "XNamespace" definition - before or after the XDocument.Load() (it should be after)
marc_s
ok with this small xml it works too. but my xml is much greater and also have other objects. see my edit. why it does not work when it finds other elements?
snarebold
now it works but when I implement a where clause it throws an error. no object instance...
snarebold
Why I could not set the where :/? Really thanks to the time you take to help me already.
snarebold
no i got it same with name space by where clauses. Can I define this for all ?
snarebold
Yes, you can define one (or several) XML namespace(s) and then use it everywhere. Shouldn't be a problem, really.
marc_s
+1  A: 

Hi

Marc has basically sorted your issues out, but i thought i'd throw in some consideration. If you have complicated XML, i would suggest using defined objects. it makes things easier in the long run.

for your example

public class dtObjektInfo
    {
        public string LanguageCode { get; set; }
        public string LiegenschaftID { get; set; }
    }

and then your code to call it would turn into

XDocument root = XDocument.Load("c:\\test.xml");
XNamespace ns = "http://tempuri.org/DataSetExchangeWMS.xsd";


dtObjektInfo objecte = (from i in root.Descendants(ns + "dtObjektInfo")

select new dtObjektInfo
{
       LanguageCode = i.Element(ns+"LanguageCode").Value,
       LiegenschaftID = i.Element(ns+"LiegenschaftID").Value 
}).First();

Then you can access the values directly from your dtObjektInfo object. makes coding a lot easier with large xml files as all the work is done in the linq query and then you have a final object.

The benefits of this is that you only have to get the linq right once. coding will be aided by the fact that you are now working with an object whose properties are known (AND INTELLISENSE YAY). all the elements can be cast to the correct types (or exceptions thrown). saving a lot of issues later.

Kamal
Thank you thats nice. And now how define the default namespace. I don't like to implement in each element the namespace.
snarebold
I actually use something like thisXElement xdb = XElement.Load(XMLPath);XNamespace NameSpace = xdb.Name.Namespace;instead of XDocument.Load(). the same query should work. i think you may not have some methods like Root, but you can treat the element as the root if you're selecting the root node. i've used this approach pretty exhaustively with pretty complicated xml and it works nicely for me
Kamal