Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / March 2008

Tip: Looking for answers? Try searching our database.

LINQ element is null

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Benn H.J. Molund - 07 Mar 2008 14:56 GMT
hi,

my xml file is:

<?xml version="1.0" encoding="ISO-8859-1"?>
<Message xmlns="http://www.kith.no/xmlstds/henvisning/2005-07-08"
xmlns:xsd="http://www.w3.org/2001/XMLSchema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.kith.no/xmlstds/henvisning/2005-07-08
Henvisning-v1.0.xsd">
    <Type V="H"/>
    <MsgVersion>v1.0 2005-07-08</MsgVersion>
    <MIGversion>v1.0 2005-07-08</MIGversion>
    <GenDate V="2005-07-06T11:30:47"/>
    <MsgId>Referansenummer123456</MsgId>
    <ServReq>
        <!--  Opplysninger om henvisningen -->
        <ServType V="N"/>
        <IssueDate V="2002-09-14"/>
        <MsgDescr V="1" DN="Utredning"/>
        <Id>FY020725A</Id>
        <Patient>
            <!--  Opplysninger om pasient -->
            <Sex V="1"/>
            <DateOfBirth V="1940-09-16"/>
            <Name>Syk, Petter</Name>
            <OffId>16094034523</OffId>
            <TypeOffId V="FNR"/>
            <!-- Kliniske opplysninger - dekker Utredningsresultater, tidligere
sykdommer, Cave og legemidler -->
            <!-- Utredningsresultater -->
            <InfItem>
                <Observation>
...
..

my C# code is:

XElement hen = XElement.Load(@"Henv-eks1-v1.0.xml");
XElement test = hen.Element("Patient");
Console.WriteLine(test.value);

says that test is null, what i'm i doing wrong??
Signature

Projectleader / .NET system devloper

Martin Honnen - 07 Mar 2008 15:16 GMT
> hi,
>
[quoted text clipped - 39 lines]
>
> says that test is null, what i'm i doing wrong??

Well first of all all elements are in the default namespace
http://www.kith.no/xmlstds/henvisning/2005-07-08 so you need
  XNamespace hv = "http://www.kith.no/xmlstds/henvisning/2005-07-08";
and then you need to qualify names e.g.
    hv + "Patient"
that you use in your code.
Additionally the Patient element is not a child of the root element but
rather of the ServReq element so you might try e.g.
  hen.Element(hv + "ServReq").Element(hv + "Patient")

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Benn H.J. Molund - 10 Mar 2008 10:16 GMT
Thx, helpet me alot:-)

Is ther a way that i can get onlye the xml tree without using the namspace?

Once again thanks for help:-)
Signature

Projectleader / .NET system devloper

> > hi,
> >
[quoted text clipped - 49 lines]
> rather of the ServReq element so you might try e.g.
>    hen.Element(hv + "ServReq").Element(hv + "Patient")
Martin Honnen - 10 Mar 2008 13:00 GMT
> Is ther a way that i can get onlye the xml tree without using the namspace?

I am not sure I understand what you want to achieve. Can you explain
"which xml tree" you are looking for?

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Benn H.J. Molund - 10 Mar 2008 13:18 GMT
Sorry, I ment all data(Elements and atributes) in the Patient part of the xml.
I asume you must ref to each element with the namespace atribute?

Signature

Projectleader / .NET system devloper

> > Is ther a way that i can get onlye the xml tree without using the namspace?
>
> I am not sure I understand what you want to achieve. Can you explain
> "which xml tree" you are looking for?
Martin Honnen - 10 Mar 2008 13:31 GMT
> Sorry, I ment all data(Elements and atributes) in the Patient part of the xml.
> I asume you must ref to each element with the namespace atribute?

Using the XNamespace is straight forward and simple.
Ignoring namespaces is possible but not very helpful in my view. If you
really want to do that:

            XElement message = XElement.Load(@"..\..\XMLFile1.xml");
            XElement patient = message.Elements().Where(el =>
el.Name.LocalName == "ServReq").Elements().Where(el => el.Name.LocalName
== "Patient").First();
            if (patient != null)
            {
                Console.WriteLine(patient);
            }
            else
            {
                Console.WriteLine("Not found.");
            }

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Benn H.J. Molund - 10 Mar 2008 14:02 GMT
aha, this was the mind opner for me :-) thx
Signature

Projectleader / .NET system devloper

> > Sorry, I ment all data(Elements and atributes) in the Patient part of the xml.
> > I asume you must ref to each element with the namespace atribute?
[quoted text clipped - 15 lines]
>                  Console.WriteLine("Not found.");
>              }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.