.net xmlserializer problem

xmlserializer problem

I am new to XMLSerializer and am trying to deserialize a very very simple xml file.
Problem is that it fails with error:
line 6, pos 5: Unknown Node:strItemName

Can anyone tell me whats wrong with my readPo() code?
Thank you

1. <?xml version="1.0" encoding="us-ascii" standalone="yes"?>
2. <?xml-stylesheet version='1.0'?>
3. <ItemList>
4. <Item>
5. <strItemName>in 1</strItemName>
6. </Item>
7. <Item>
8. <strItemName>in 2</strItemName>
9. </Item>
10. </ItemList>

read it into code:

// ret true if read ok else false if error
private static bool bReadPO ( string filename, ref ItemList MyItemList )
{
m_bGotXmlSerializeError = false; // init
XmlSerializer serializer = new XmlSerializer ( typeof ( ItemList ) );

/* If the XML document has been altered with unknown
nodes or attributes, handle them with the
UnknownNode and UnknownAttribute events.*/

serializer.UnknownNode += new XmlNodeEventHandler ( serializer_UnknownNode );
serializer.UnknownAttribute += new XmlAttributeEventHandler ( serializer_UnknownAttribute );

// A FileStream is needed to read the XML document.

FileStream fs = new FileStream ( filename, FileMode.Open );

MyItemList = (ItemList) serializer.Deserialize ( fs );

return ( !m_bGotXmlSerializeError );
}

private static void serializer_UnknownNode ( object sender, XmlNodeEventArgs e )
{
if ( m_bGotXmlSerializeError == false ) {

Debug.WriteLine ( "line " + e.LineNumber + ", pos " + e.LinePosition + ": Unknown Node:" + e.Name + "\t" + e.Text );
Console.WriteLine ( "line " + e.LineNumber + ", pos " + e.LinePosition + ": Unknown Node:" + e.Name + "\t" + e.Text );

m_bGotXmlSerializeError = true;

} else {
// already got error
;
}

return;
}

private static void serializer_UnknownAttribute (object sender, XmlAttributeEventArgs e )
{
System.Xml.XmlAttribute attr = e.Attr;

if ( m_bGotXmlSerializeError == false ) {

Debug.WriteLine ( "line " + e.LineNumber + ", pos " + e.LinePosition + ": Unknown attribute " + attr.Name + "='" + attr.Value + "'");
Console.WriteLine ( "line " + e.LineNumber + ", pos " + e.LinePosition + ": Unknown attribute " + attr.Name + "='" + attr.Value + "'");

m_bGotXmlSerializeError = true;

} else {
// already got error
;
}

return;
}
[2841 byte] By [pschiff] at [2007-11-19 19:12:38]
# 1 Re: .net xmlserializer problem
Sounds like your xml doesn't match the schema for an ItemList.
Have you tried creating an ItemList, serializing it and seeing the output?
jkmyoung at 2007-11-10 3:27:03 >
# 2 Re: .net xmlserializer problem
pardon my ignorance here but just how would i serialize the ItemList to an xml file?
pschiff at 2007-11-10 3:28:03 >
# 3 Re: .net xmlserializer problem
Use your serializer you created here:
XmlSerializer serializer = new XmlSerializer ( typeof ( ItemList ) );

Create your ItemList (personally not familiar with this class). Say, you create an ItemList i. Add a couple elements to it.

You could write to a temporary file:
TextWriter writer = new StreamWriter("temp.txt");
serializer.Serialize(writer, i);
jkmyoung at 2007-11-10 3:29:08 >
# 4 Re: .net xmlserializer problem
Ok
I now use my C# code to generate the below XML file. Then I later attempt to validate it. When I attempt to validate my below xml file with the below xsd file, i get this error.
Any ideas as to whats wrong with the xsd?

"got exception The 'http://tempuri.org/WDWEBSVC.xsd:strItemName' element is not declared. An error occurred at file:///C:/JUNK/DEV/WATCHDOG/WDWEBSVC/WDCLIENT/bin/Debug/WDWEBSVC.XSD, (19, 22)."

--------

<?xml version="1.0" encoding="utf-8"?>
<OurRootClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/WDWEBSVC.xsd">
<Items>
<Item>
<strItemName xmlns="http://www.aspnetsbs.com/webservices/">Ping Nifty Tools</strItemName>
<strItemAddress xmlns="http://www.aspnetsbs.com/webservices/">www.niftytools.com</strItemAddress>
</Item>
<Item>
<strItemName xmlns="http://www.aspnetsbs.com/webservices/">Html download Nifty Tools</strItemName>
<strItemAddress xmlns="http://www.aspnetsbs.com/webservices/">http://www.niftytools.com</strItemAddress>
</Item>
</Items>
</OurRootClass>

-------

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="OurRootClass"
targetNamespace="http://tempuri.org/WDWEBSVC.xsd"
xmlns:mstns="http://tempuri.org/WDWEBSVC.xsd"
xmlns="http://tempuri.org/WDWEBSVC.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
attributeFormDefault="qualified"
elementFormDefault="qualified">
<xs:element name="OurRootClass" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Items">
<xs:complexType>
<xs:sequence>
<xs:element name="Item" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element ref="strItemName" minOccurs="0" />
<xs:element ref="strItemAddress" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
pschiff at 2007-11-10 3:30:13 >
# 5 Re: .net xmlserializer problem
<strItemName xmlns="http://www.aspnetsbs.com/webservices/">Ping Nifty Tools</strItemName>

The serializer is adding it's own namespace to these elements, so once again your schema doesn't understand it.

Add the namespace xmlns="http://www.aspnetsbs.com/webservices/" to your strItemName schema.

If you add this nameSpace to your original xml, it'll probably deserialize properly.
jkmyoung at 2007-11-10 3:31:12 >