ikorin24 / U8XmlParser

Extremely fast UTF-8 xml parser library
MIT License
95 stars 13 forks source link

XmlNode.OuterXml #15

Closed simon-curtis closed 2 years ago

simon-curtis commented 2 years ago

I am trying to get the outer xml of a node, current it looks like you only have InnerText. is this something that could be added?

EDIT:

An example would be

<Messages>
    <Message Id="1">
        <Name>Foo</Name>
    </Message>
    <Message Id="2">
        <Name>Foo</Name>
    </Message>
</Messages>

And the result of outerxml selecting the first message in Messages.Children should be:

<Message Id="1">
    <Name>Foo</Name>
</Message>
ikorin24 commented 2 years ago

I will add XmlNode.AsRawString(). Is this the function you are looking for?

public struct XmlNode
{
    public RawString AsRawString();
}
<Messages>
    <Message Id="1">
        <Name>Foo</Name>
    </Message>
    <Message Id="2">
        <Name>Foo</Name>
    </Message>
</Messages>
using XmlObject xml = XmlParser.Parse(XmlString);
XmlNode node = xml.FindChild("Message");     // <Message Id="1">

RawString nodeString = node.AsRawString();
/*
nodeString is

"<Message Id="1">
        <Name>Foo</Name>
    </Message>"

*/
simon-curtis commented 2 years ago

Excellent! Yes, it does. But you might be able to tell me if what I'm after is not actually what I want:

I receive a submission document that contains multiple messages. When processing we split the messages up and then use the submission document as a wrapper around each message.

<Submission>
    <Header>...</Header>
    <Messages>
       <Message>...</Message>
       <Message>...</Message>
       <Message>...</Message>
    </Messages>
</Submission>

This then becomes (for each message):

<Submission>
    <Header>...</Header>
    <Messages>
       <Message>...</Message>
    </Messages>
</Submission>

I have moved feom XmlDocument to XDocument which has nearly halved the speed, but hasn't done much for the allocations.

I am worried though that in doing this with my aforementioned api request with your library is going to significantly increase string allocations instead when building the new documents 🤔

ikorin24 commented 2 years ago

OK, I understand what you want to do.

My new method works without new allocation. It is a slice of the xml.

U8XmlParser is just a parser, which provides the function for reading. All data U8XmlParser provides are readonly. So you have to use other libraries to create or write a new xml.

So, whether the memory allocation will increase or not depends on the library that creates the xml.

A new version will be released in a few days. Please give it a try. Thank you.