-1

I have a C# program that attempts to read the following xml, but can't read any elements:

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Comments Here -->
    <FileFeed 
    xmlns="http://www.mycompany.com/schemas/xxx/FileFeed/V1" 
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.somecompany.com/schemas/xxx/FileFeed/V1 
    FileFeed.xsd" 
    RecordCount = "1">
<Object>
    <ID>PAMSMOKE110113xxx</ID>
    <CorpID>12509</CorpID>
    <AnotherID>201654702345</AnotherID>
    <TimeStamp>2013-09-03</TimeStamp>
    <Type>Some Type</Type>
    <SIM_ID>89011704258012600767</SIM_ID>
    <Code>ZZZ</Code>
    <Year>2013</Year>
</Object>
</FileFeed>

With the above XML my C# program is unable to read any elements.. For instance the ID Element is always NULL.

Now if I simply remove the first xmlns from the above XML, my program can read all the elements without any issues. The problem is I have to process the XML file in the format that's given to me, and can't change the file format. My program reads the below XML just fine: Note the line xmlns="http://www.mycompany.com/schemas/xxx/FileFeed/V1" is removed.

   <?xml version="1.0" encoding="UTF-8"?>
    <!-- Comments Here -->
    <FileFeed 
        xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.somecompany.com/schemas/xxx/FileFeed/V1        
        FileFeed.xsd" 
        RecordCount = "1">
        <Object>
            <ID>PAMSMOKE110113xxx</ID>
            <CorpID>12509</CorpID>
            <AnotherID>201654702345</AnotherID>
            <TimeStamp>2013-09-03</TimeStamp>
            <Type>Some Type</Type>
            <SomeNumber>89011704258012600767</SomeNumber>
            <Code>ZZZ</Code>
            <Year>2013</Year>
        </Object>
    </FileFeed>

I realize I'm not posting any code, but just wondering what possible issue could I be having, where simply removing the xmlns line resolves everything??

pnuts
  • 58,317
  • 11
  • 87
  • 139
Jack Allen
  • 103
  • 1
  • 7
  • Is that the actual URL? It seems that it has a whitespace in the middle, if it is supposed to be here try replacing it with `%20` – SJuan76 Nov 02 '13 at 20:15
  • 1
    -1: There are many duplicates for this - search for "namespace XML" to find them. I.e. http://stackoverflow.com/questions/2340411/use-linq-to-xml-with-xml-namespaces/2340497#2340497 (if using LINQ-toXML). – Alexei Levenkov Nov 02 '13 at 20:15
  • How are you reading the XML? – Rowland Shaw Nov 02 '13 at 20:17

3 Answers3

1

Your problem is with xml namespaces

Using Linq2Xml

XNamespace ns = "http://www.mycompany.com/schemas/xxx/FileFeed/V1";
var xDoc = XDocument.Load(fname);
var id = xDoc.Root.Element(ns + "Object").Element(ns + "ID").Value;
L.B
  • 114,136
  • 19
  • 178
  • 224
1

Your root element FileFeed has a namespace attribute. This means that each element inside it also uses that namespace.

The Element method takes an XName as its argument. Usually you use a string which gets implicitly converted into an XName.

If you want to include a namespace you create an XNamespace and add the string. Since XNamespace overloads the + operator this will also result in an XName.

XDocument doc = XDocument.Load("Test.xml");

// this will be null
XElement objectElementWithoutNS = doc.Root.Element("Object");

XNamespace ns = doc.Root.GetDefaultNamespace();
XElement objectElementWithNS = doc.Root.Element(ns + "Object");
pescolino
  • 3,086
  • 2
  • 14
  • 24
-1

Xml namespaces are more or less like C# namespaces. Would you be able to access a class when its namespace is set or not set?

public namespace My.Company.Schemas {
   public class FileFeed

vs

public class FileFeed {

They are two DISTINCT classes! The same applies to XML - by setting a namespace you make it possible to have documents with similar or even the same internal structure but they represent two disctinct documents that are not exchangeable. This is really convenient.

If you'd like to get help on why your actual reading method doesn't consider the namespace, you have to present the C# code. The general rule though is that any reading API makes is possible to set the namespace for actual reading.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
  • Thanks Everyone, you were right on. Simply had to add the following line to my code: XNamespace ns = "http://www.MyCompany.com/schemas/xxx/VehicleInstallFeed/V1"; Then had to do the following to be able to correctly read the element values: object.Element(ns + "ElementName"). Really appreciate everyone's feedback. – Jack Allen Nov 03 '13 at 19:38