Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

Monday, March 26, 2012

Stringbuilder equivalent to XMLTextWriter?

I want to write an XML file but not to a file...just into memory, so I can
then pass it to a string and stick it in a database.

Seems that XMLtextWriter requires that I write to a filestream.

What I'm looking for is the ease of writing XML that XMLTextWriter has with
the ability to just write to memory like Stringbuilder can do. Is there such
a thing?

-Darrel> What I'm looking for is the ease of writing XML that XMLTextWriter has
> with the ability to just write to memory like Stringbuilder can do. Is
> there such a thing?

Hmm...I found this article:

http://www.15seconds.com/issue/050615.htm

Which mentions:

"the XmlWriter can generate its output as a disk file, a stream, a
StringBuilder or another writer instance."

So, it looks like it can generate a stringbuilder. I just need to find an
example of that... ;o)

-Darrel
> So, it looks like it can generate a stringbuilder. I just need to find an
> example of that... ;o)

A bit more digging and I think I've figure it out:

Dim sbXML As New System.Text.StringBuilder

Dim swXML As New System.IO.StringWriter(sbXML)

Dim twXML As New System.xml.XmlTextWriter(swXML)

-Darrel

Stringbuilder equivalent to XMLTextWriter?

I want to write an XML file but not to a file...just into memory, so I can
then pass it to a string and stick it in a database.
Seems that XMLtextWriter requires that I write to a filestream.
What I'm looking for is the ease of writing XML that XMLTextWriter has with
the ability to just write to memory like Stringbuilder can do. Is there such
a thing?
-Darrel> What I'm looking for is the ease of writing XML that XMLTextWriter has
> with the ability to just write to memory like Stringbuilder can do. Is
> there such a thing?
Hmm...I found this article:
http://www.15seconds.com/issue/050615.htm
Which mentions:
"the XmlWriter can generate its output as a disk file, a stream, a
StringBuilder or another writer instance."
So, it looks like it can generate a stringbuilder. I just need to find an
example of that... ;o)
-Darrel

> So, it looks like it can generate a stringbuilder. I just need to find an
> example of that... ;o)
A bit more digging and I think I've figure it out:
Dim sbXML As New System.Text.StringBuilder
Dim swXML As New System.IO.StringWriter(sbXML)
Dim twXML As New System.xml.XmlTextWriter(swXML)
-Darrel

Thursday, March 22, 2012

Strongly typed datasets and XML

I am having trouble understanding strongly typed datasets and XML files. I
have the following schema:

<xs:schema id="Pages" targetNamespace="http://asdf.org/EOBEPages.xsd"
elementFormDefault="qualified"
xmlns="http://asdf.org" xmlns:mstns="http://asdf.org/EOBEPages.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="EOBEPages">
<xs:complexType>
<xs:sequence>
<xs:element name="Page">
<xs:complexType>
<xs:sequence>
<xs:element name="PageTitle" type="xs:string" />
<xs:element name="PageText" type="xs:string" />
<xs:element name="PageNumber" type="xs:integer" />
<xs:element name="NumImages" type="xs:integer" />
<xs:element name="ImagesDescription" type="xs:string" minOccurs="0"
maxOccurs="1" />
<xs:element name="ImageDefinitions">
<xs:complexType>
<xs:sequence>
<xs:element name="Description" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema
I load the xml data into the dataset no problem, and can access the "page"
info by:

myDataset.Page(index).pageTitle

, etc, but can NOT access the "ImageDefinitions", because I guess I don't
understand how. What I wanted to do is something like:

myVar = myDataset.Page(index).ImageDefinitions(index2).Des cription

but this is not part of the dataset... why? How can I get access to the
image definition descriptions based on the page index?

Thanks a bunch!

MC Ddatasets are tables with relationships. every xml complex type becomes a new
table. ImageDefinitions becomes a table which you can access.

myVar = myDataset.ImageDefinitions(index).Description

if you have a relionship setup you can access ImageDefinitions rows from
Page rows using GetChildRows

-- bruce (sqlwork.com)

"Big D" <a@.a.com> wrote in message
news:uam6WMY9DHA.1632@.TK2MSFTNGP12.phx.gbl...
> I am having trouble understanding strongly typed datasets and XML files.
I
> have the following schema:
> <xs:schema id="Pages" targetNamespace="http://asdf.org/EOBEPages.xsd"
> elementFormDefault="qualified"
> xmlns="http://asdf.org" xmlns:mstns="http://asdf.org/EOBEPages.xsd"
> xmlns:xs="http://www.w3.org/2001/XMLSchema">
> <xs:element name="EOBEPages">
> <xs:complexType>
> <xs:sequence
> <xs:element name="Page">
> <xs:complexType>
> <xs:sequence>
> <xs:element name="PageTitle" type="xs:string" />
> <xs:element name="PageText" type="xs:string" />
> <xs:element name="PageNumber" type="xs:integer" />
> <xs:element name="NumImages" type="xs:integer" />
> <xs:element name="ImagesDescription" type="xs:string" minOccurs="0"
> maxOccurs="1" /
> <xs:element name="ImageDefinitions">
> <xs:complexType>
> <xs:sequence>
> <xs:element name="Description" type="xs:string" />
> </xs:sequence>
> </xs:complexType>
> </xs:element>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
> </xs:schema>
> I load the xml data into the dataset no problem, and can access the "page"
> info by:
> myDataset.Page(index).pageTitle
> , etc, but can NOT access the "ImageDefinitions", because I guess I don't
> understand how. What I wanted to do is something like:
> myVar = myDataset.Page(index).ImageDefinitions(index2).Des cription
> but this is not part of the dataset... why? How can I get access to the
> image definition descriptions based on the page index?
> Thanks a bunch!
> MC D

Tuesday, March 13, 2012

strugling with Atributed XML parsing.

I have XML in following format
<ORDER>
<HEADER> ... </HEADER>
<LINE> ... <LINE>
<LINE> ... <LINE>
<LINE> ... <LINE>
</ORDER
Can someone suggest how will i parseout such document using XmlSerializer
and attributes.
There can be different amount of lines in different documents.

I have

class clsOrder

{

clsHeader _header;

clsLine [] Lines;

}

How do i specify that <line> goes into Lines array?

Thanks.

George.Hi George,

Thanks for posting in the community!
From your description, you need some suggestions or information on how to
specify the proper XmlAttributes in certian classes so as to use
XmlSerializer to output them and the certain custom class you made contains
an array member of another custom class, yes?
If there is anything I misunderstood, please feel free to let me know.

As for this question, I suggest that you use the "XmlArray" and
XmlArrayItem" attribute to implement the control of the array member within
a certain class's serialization. For example, we can provide the class's
code as below:

public class clsLine
{
public clsLine()
{}

public string Name = "name";
public string Description = "description";
}

public class clsOrder
{
public clsOrder()
{}

[XmlElement("MyHeader")]
public string Header = "header";

[XmlArrayItem(typeof(clsLine),ElementName = "MyLine")]
[XmlArray(ElementName = "MyLines",Namespace = "", IsNullable = true)]
public clsLine[] Lines = null;

}

Notice the two attributes specified in the clsOrder class.
1. the "XmlArray" has specified what name to use for the array member when
the class's instance is serialized, here I specify it as "MyLines", and the
"XmlArrayItem" can be used to specify the array member's item's serialize
options, and when i serialzie a certain clsOrder instance via the following
code:
----------------
XmlSerializer serializer = new XmlSerializer(typeof(clsOrder));

clsOrder odr = new clsOrder();
odr.Header = "Test Order";
clsLine[] lines = new clsLine[5];

for(int i=0;i<lines.Length;i++)
{
clsLine line = new clsLine();
line.Name = "Name" + i.ToString();
line.Description = "Description" + i.ToString();
lines[i] = line;
}

odr.Lines = lines;

TextWriter writer = new StreamWriter("output.xml");
serializer.Serialize(writer,odr);
writer.Close();
------------
The output.xml is like:
----------output.xml--------
<?xml version="1.0" encoding="utf-8"?>
<clsOrder xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MyHeader>Test Order</MyHeader>
<MyLines>
<MyLine>
<Name>Name0</Name>
<Description>Description0</Description>
</MyLine>
<MyLine>
<Name>Name1</Name>
<Description>Description1</Description>
</MyLine>
<MyLine>
<Name>Name2</Name>
<Description>Description2</Description>
</MyLine>
<MyLine>
<Name>Name3</Name>
<Description>Description3</Description>
</MyLine>
<MyLine>
<Name>Name4</Name>
<Description>Description4</Description>
</MyLine>
</MyLines>
</clsOrder>
----------------

In addition, here is some tech references in MSDN on the how to use and
control the XmlSerializer, I believe they'll be helpful to you:

#Introducing XML Serialization
http://msdn.microsoft.com/library/e...roducingxmlseri
alization.asp?frame=true

#Examples of XML Serialization
http://msdn.microsoft.com/library/e...xampleofxmlseri
alizationwithxmlserializer.asp?frame=true

#Controlling XML Serialization Using Attributes
http://msdn.microsoft.com/library/e...trollingseriali
zationbyxmlserializerwithattributes.asp?frame=true

#Attributes That Control XML Serialization
http://msdn.microsoft.com/library/e...ributesthatcont
rolserialization.asp?frame=true

Please check out my preceding suggestions, if you have any questions on
them, please feel free to let me know.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
That is the problem.
Your sample creates following XML
<clsOrder>
<MyHeader>Test Order</MyHeader>
<MyLines>
<MyLine> ..</MyLine>
<MyLine> ..</MyLine>
<MyLine> ..</MyLine>
</MyLines>
</clsOrder
Unfortunately i need following structure.

<clsOrder>
<MyHeader>Test Order</MyHeader>
<MyLine> ..</MyLine>
<MyLine> ..</MyLine>
<MyLine> ..</MyLine>
</clsOrder
Notice there is no <MyLines> tag

I do agree that your structure is more natural and i would not even ask my
question if that was a case. But i stuck with aftermarket standart which was
created by some guys with jumbo heads (joke) and need to be able to parse
that XML

Thanks.
George.

"Steven Cheng[MSFT]" <v-schang@.online.microsoft.com> wrote in message
news:DaO7nzf6DHA.3736@.cpmsftngxa07.phx.gbl...
> Hi George,
>
> Thanks for posting in the community!
> From your description, you need some suggestions or information on how to
> specify the proper XmlAttributes in certian classes so as to use
> XmlSerializer to output them and the certain custom class you made
contains
> an array member of another custom class, yes?
> If there is anything I misunderstood, please feel free to let me know.
> As for this question, I suggest that you use the "XmlArray" and
> XmlArrayItem" attribute to implement the control of the array member
within
> a certain class's serialization. For example, we can provide the class's
> code as below:
>
> public class clsLine
> {
> public clsLine()
> {}
> public string Name = "name";
> public string Description = "description";
> }
> public class clsOrder
> {
> public clsOrder()
> {}
> [XmlElement("MyHeader")]
> public string Header = "header";
> [XmlArrayItem(typeof(clsLine),ElementName = "MyLine")]
> [XmlArray(ElementName = "MyLines",Namespace = "", IsNullable = true)]
> public clsLine[] Lines = null;
> }
> Notice the two attributes specified in the clsOrder class.
> 1. the "XmlArray" has specified what name to use for the array member when
> the class's instance is serialized, here I specify it as "MyLines", and
the
> "XmlArrayItem" can be used to specify the array member's item's serialize
> options, and when i serialzie a certain clsOrder instance via the
following
> code:
> ----------------
> XmlSerializer serializer = new XmlSerializer(typeof(clsOrder));
> clsOrder odr = new clsOrder();
> odr.Header = "Test Order";
> clsLine[] lines = new clsLine[5];
> for(int i=0;i<lines.Length;i++)
> {
> clsLine line = new clsLine();
> line.Name = "Name" + i.ToString();
> line.Description = "Description" + i.ToString();
> lines[i] = line;
> }
> odr.Lines = lines;
> TextWriter writer = new StreamWriter("output.xml");
> serializer.Serialize(writer,odr);
> writer.Close();
> ------------
> The output.xml is like:
> ----------output.xml--------
> <?xml version="1.0" encoding="utf-8"?>
> <clsOrder xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
> <MyHeader>Test Order</MyHeader>
> <MyLines>
> <MyLine>
> <Name>Name0</Name>
> <Description>Description0</Description>
> </MyLine>
> <MyLine>
> <Name>Name1</Name>
> <Description>Description1</Description>
> </MyLine>
> <MyLine>
> <Name>Name2</Name>
> <Description>Description2</Description>
> </MyLine>
> <MyLine>
> <Name>Name3</Name>
> <Description>Description3</Description>
> </MyLine>
> <MyLine>
> <Name>Name4</Name>
> <Description>Description4</Description>
> </MyLine>
> </MyLines>
> </clsOrder>
> ----------------
> In addition, here is some tech references in MSDN on the how to use and
> control the XmlSerializer, I believe they'll be helpful to you:
> #Introducing XML Serialization
http://msdn.microsoft.com/library/e...roducingxmlseri
> alization.asp?frame=true
> #Examples of XML Serialization
http://msdn.microsoft.com/library/e...xampleofxmlseri
> alizationwithxmlserializer.asp?frame=true
> #Controlling XML Serialization Using Attributes
http://msdn.microsoft.com/library/e...trollingseriali
> zationbyxmlserializerwithattributes.asp?frame=true
> #Attributes That Control XML Serialization
http://msdn.microsoft.com/library/e...ributesthatcont
> rolserialization.asp?frame=true
> Please check out my preceding suggestions, if you have any questions on
> them, please feel free to let me know.
>
> Regards,
> Steven Cheng
> Microsoft Online Support
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
Hi George,

Thank you for the response. Regarding on the issue, I am
finding proper resource to assist you and we will update as soon as posible.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security(This posting is provided "AS IS",
with no warranties, and confers no rights.)
Hi George,

We are still researching this issue. We will post more information as soon
as we can.

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computers security.

This posting is provided "AS IS", with no warranties, and confers no rights.

-------
> X-Tomcat-ID: 199496731
> References: <OxJQSeZ6DHA.2496@.TK2MSFTNGP09.phx.gbl>
<DaO7nzf6DHA.3736@.cpmsftngxa07.phx.gbl>
<u4mVwal6DHA.1428@.TK2MSFTNGP12.phx.gbl>
> MIME-Version: 1.0
> Content-Type: text/plain
> Content-Transfer-Encoding: 7bit
> From: v-schang@.online.microsoft.com (Steven Cheng[MSFT])
> Organization: Microsoft
> Date: Wed, 04 Feb 2004 11:54:58 GMT
> Subject: Re: strugling with Atributed XML parsing.
> X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
> Message-ID: <wwyD$Wx6DHA.1988@.cpmsftngxa07.phx.gbl>
> Newsgroups: microsoft.public.dotnet.framework.aspnet
> Lines: 9
> Path: cpmsftngxa07.phx.gbl
> Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet:207429
> NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
> Hi George,
> Thank you for the response. Regarding on the issue, I am
> finding proper resource to assist you and we will update as soon as
posible.
>
> Regards,
> Steven Cheng
> Microsoft Online Support
> Get Secure! www.microsoft.com/security(This posting is provided "AS IS",
> with no warranties, and confers no rights.)
I used the Xml Schema tool(xsd.exe) to automatically generate the schema
and serialize mapping class. To my surprise, the tool generated class
worked. Here is the xml format I used as example to generate class:
<clsOrder>
<MyHeader>Test Order</MyHeader>
<MyLine> dsfsdffd</MyLine>
<MyLine>dsfsdf</MyLine>
<MyLine> fdfdsf</MyLine>
</clsOrder
Then, I used xsd.exe to generate the schema and class ,here is the
generated class:

[System.Xml.Serialization.XmlRootAttribute(Namespac e="",
IsNullable=false)] public class clsOrder {

[System.Xml.Serialization.XmlElementAttribute(Form= System.Xml.Schema.XmlSche
maForm.Unqualified)]
public string MyHeader;

[System.Xml.Serialization.XmlElementAttribute("MyLine",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
public clsOrderMyLine[] MyLine;
}

public class clsOrderMyLine {

[System.Xml.Serialization.XmlTextAttribute()]
public string Value;
}

[System.Xml.Serialization.XmlRootAttribute(Namespac e="",
IsNullable=false)] public class NewDataSet {

[System.Xml.Serialization.XmlElementAttribute("clsOrder")]
public clsOrder[] Items;
}

It could serialize and deserialize with the xml format the customer exactly
want. Hope this help!

-Thank you
Madhu
Microsoft Developer Engineer

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
closing this thread

Stuck on XML/XSLT with Asp.net 1.1 server controls.

Hello everyone,

I am working on a custom built portal system in Visual Studio 2003 and I amstuck on how to develop with XML/XSLT and using Asp.net server controls. Theportal consists of "applications" (each application is essentially a.aspx page) and those applications consists of "modules" (whichderives from UserControl). Now in order to maintain a consistent look for allmodules/applications and to minimize the amount of actual html hard coded, Iwant to use XSLT (which would contain the html) and CSS to help generate thatconsistent appearance. TheModule class containstitle andbodyfields, wherebodycan have any form of content (html/text/javascript),but sinceModulederives from UserControl, it can also contain .Net childcontrols, and I need those child controls to render correctly to take fulladvantage of Asp.Net. What would be the best approach to accomplish this inwhich the module would be transformed using a XSL file?


This also needs to apply to an application as well (or atleast the layout of the application, something that controls the placement ofthe modules). If I have a collection of modules, I need to put them in someform of structure or layout and transform that layout through an XSL file tohave a consistent look and only one place to look when we need to change thelook of a module or application. What would be the best approach for this orcould you point me in the right direction? If you need any furtherclarification, I would be more than willing to help. Thanks in advance!

Steve

Wow that was a lot...

Was there a specific bit of coding that you are stuck on or are you looking for a broadranged theory?


If you'd like I can tell you what I have right now, but I'mthinking it may be better to take a step back and look at it from a differentangle.Smile [:)] The first question is best approach to take a user control that contains child controls and other content, run it through an .xsl file, and have it display properly on the page. Thanks for your help!


User controls containing child controls is nothing special... in fact really just about every User Control ever made has child controls. User Controls, with User Controls would be nothing different really either...

Let me see if I have it right, before I comment...

You want your XML to contain the list of what's on each control, and the XSL to contain the "way" that the controls display? Is that correct?

And you are wondering if this is a good approach?